Skip to content

Commit

Permalink
fix: fixed primativeKeyStringDencoder() decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed Sep 19, 2022
1 parent a927ed2 commit 427faf1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,24 +419,69 @@ describe('firestoreDencoderArray()', () => {
});
});

export type TestDencoderRole = GrantedReadRole | GrantedUpdateRole;

export enum TestDencoderRoleCodes {
READ = 'r',
UPDATE = 'u'
}

export const ROLE_CODE_MAP: PrimativeKeyDencoderValueMap<TestDencoderRole, TestDencoderRoleCodes> = {
r: 'read',
u: 'update'
};

export const ROLE_DENCODER = primativeKeyStringDencoder({
dencoder: primativeKeyDencoder({
values: ROLE_CODE_MAP
})
});

export interface TestDencoderStringArrayObject {
value: TestDencoderRole[];
}

describe('firestoreDencoderStringArray()', () => {
const firestoreMapConfig = firestoreDencoderStringArray<number, string>({
dencoder: primativeKeyStringDencoder<number, string>({
dencoder: {
values: {
a: 0,
b: 1
}
}
})
const firestoreDencoderField = firestoreDencoderStringArray({
dencoder: ROLE_DENCODER
});

it('should encode the values.', () => {
const test = [0, 1, 2];
const results = firestoreMapConfig.to.convert(test);
const test: TestDencoderRole[] = ['read', 'update'];
const results = firestoreDencoderField.to.convert(test);

expect(results).toBeDefined();
expect(results).toBe('ab');
expect(results).toBe('ru');
});

describe('converter', () => {
const converter = snapshotConverterFunctions<TestDencoderStringArrayObject>({
fields: {
value: firestoreDencoderField
}
});

it('should encode the values.', () => {
const object: TestDencoderStringArrayObject = {
value: ['read', 'update']
};

const result = converter.to(object);

expect(result.value).toBe('ru');
});

it('should decode the values.', () => {
const object: TestDencoderStringArrayObject = {
value: ['read', 'update']
};

const encoded = converter.to(object);
const decoded = converter.mapFunctions.from(encoded);

expect(decoded.value).toContain('read');
expect(decoded.value).toContain('update');
});
});
});

Expand Down
20 changes: 20 additions & 0 deletions packages/util/src/lib/string/dencoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ describe('primativeKeyStringDencoder()', () => {
const result = fn(input);
expect(result).toBe(`${TestEncodedValuesShort.TEST_A}${TestEncodedValuesShort.TEST_B}`);
});

it('should encode and decode the input values', () => {
const input = ['testa', 'testb'];

const encoded = fn(input);
const decoded = fn(encoded);

expect(decoded).toContain(input[0]);
expect(decoded).toContain(input[1]);
});
});
});

Expand Down Expand Up @@ -149,6 +159,16 @@ describe('primativeKeyStringDencoder()', () => {
const result = fn(input);
expect(result).toBe(`${TestEncodedValuesLong.TEST_A_L}${splitter}${TestEncodedValuesLong.TEST_B_L}`);
});

it('should encode and decode the input values', () => {
const input = ['testa', 'testb'];

const encoded = fn(input);
const decoded = fn(encoded);

expect(decoded).toContain(input[0]);
expect(decoded).toContain(input[1]);
});
});
});
});
4 changes: 2 additions & 2 deletions packages/util/src/lib/string/dencoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FactoryWithRequiredInput, forEachKeyValue, KeyValueTypleValueFilter, PrimativeKey, ArrayOrValue, Maybe, filterMaybeValues } from '@dereekb/util';
import { FactoryWithRequiredInput, forEachKeyValue, KeyValueTypleValueFilter, PrimativeKey, ArrayOrValue, Maybe, filterMaybeValues, iterableToArray } from '@dereekb/util';
import { Writable } from 'ts-essentials';

/**
Expand Down Expand Up @@ -155,7 +155,7 @@ export function primativeKeyStringDencoder<D extends PrimativeKey, E extends Pri

const joiner = splitter || '';

const splitEncodedValues = splitter ? (encodedValues: string) => encodedValues.split(splitter) : (encodedValues: string) => new Array(encodedValues);
const splitEncodedValues = splitter ? (encodedValues: string) => encodedValues.split(splitter) : (encodedValues: string) => Array.from(encodedValues);

return (input: string | (E | D)[]) => {
if (typeof input === 'string') {
Expand Down

0 comments on commit 427faf1

Please sign in to comment.