implement kvp-array.ts utility
This commit is contained in:
parent
457fc38e6a
commit
5a28c4e110
2 changed files with 126 additions and 0 deletions
78
packages/backend/test/unit/misc/kvp-array.ts
Normal file
78
packages/backend/test/unit/misc/kvp-array.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { makeKVPArray } from '@/misc/kvp-array.js';
|
||||
|
||||
describe(makeKVPArray, () => {
|
||||
it('should add keys property', () => {
|
||||
const array = [['1', 1], ['2', 2], ['3', 3]] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result).toHaveProperty('keys');
|
||||
});
|
||||
|
||||
it('should add values property', () => {
|
||||
const array = [['1', 1], ['2', 2], ['3', 3]] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result).toHaveProperty('values');
|
||||
});
|
||||
|
||||
it('should preserve values', () => {
|
||||
const array: [string, number][] = [['1', 1], ['2', 2], ['3', 3]];
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result).toEqual(array);
|
||||
});
|
||||
|
||||
it('should accept empty array', () => {
|
||||
const array = [] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result).toHaveProperty('keys');
|
||||
expect(result).toHaveProperty('values');
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('keys', () => {
|
||||
it('should return all keys', () => {
|
||||
const array = [['1', 1], ['2', 2], ['3', 3]] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result.keys).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it('should preserve duplicates', () => {
|
||||
const array = [['1', 1], ['1', 1], ['1', 1]] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result.keys).toEqual(['1', '1', '1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('values', () => {
|
||||
it('should return all values', () => {
|
||||
const array = [['1', 1], ['2', 2], ['3', 3]] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result.values).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should preserve duplicates', () => {
|
||||
const array = [['1', 1], ['1', 1], ['1', 1]] as const;
|
||||
|
||||
const result = makeKVPArray(array);
|
||||
|
||||
expect(result.values).toEqual([1, 1, 1]);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue