Skip to content

Commit

Permalink
fix(kv): Sort key/value data (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jan 1, 2025
1 parent e5c1d9e commit bbd2c6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ export function write(container: KTX2Container, options: WriteOptions = {}): Uin
///////////////////////////////////////////////////

const keyValueData: Uint8Array[] = [];
let keyValue = container.keyValue;
const keyValueList = Object.entries({
...container.keyValue,
...(!options.keepWriter && { KTXwriter: KTX_WRITER }),
});

if (!options.keepWriter) {
keyValue = { ...container.keyValue, KTXwriter: KTX_WRITER };
}
keyValueList.sort((a, b) => (a[0] > b[0] ? 1 : -1));

for (const key in keyValue) {
const value = keyValue[key];
for (const [key, value] of keyValueList) {
const keyData = encodeText(key);
const valueData = typeof value === 'string' ? concat([encodeText(value), NUL]) : value;
const kvByteLength = keyData.byteLength + 1 + valueData.byteLength;
Expand Down
13 changes: 13 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ test('read kv', (t) => {
);
});

test('sort kv', (t) => {
const a = read(SAMPLE_ETC1S);
a.keyValue = {
b: '123',
a: '456',
c: '789',
ab: '012',
};
const b = read(write(a));

t.deepEqual(Object.keys(b.keyValue), ['KTXwriter', 'a', 'ab', 'b', 'c'], 'sorted keys');
});

test('createDefaultContainer', (t) => {
const container = createDefaultContainer();

Expand Down

0 comments on commit bbd2c6d

Please sign in to comment.