-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80754d2
commit beeadbc
Showing
8 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@xobj/core": minor | ||
--- | ||
|
||
Add replacer via function map and entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export type ReplacerMethod = (value: any) => any; | ||
|
||
export type MapEntries<K, V> = readonly (readonly [K, V])[]; | ||
|
||
export type ReplacerType = ReplacerMethod | Map<any, any> | MapEntries<any, any>; | ||
|
||
export const defaultReplacer: ReplacerMethod = (value) => value; | ||
|
||
export function replacerFromTable(table: Map<any, any>): ReplacerMethod { | ||
return (value) => { | ||
if (table.has(value)) return table.get(value); | ||
return value; | ||
}; | ||
} | ||
|
||
export function getReplacer(replacerOption: ReplacerType | undefined): ReplacerMethod { | ||
if (!replacerOption) { | ||
return defaultReplacer; | ||
} | ||
|
||
if (typeof replacerOption === 'function') { | ||
return replacerOption; | ||
} | ||
|
||
if (replacerOption instanceof Map) { | ||
return replacerFromTable(replacerOption); | ||
} | ||
|
||
if (Array.isArray(replacerOption)) { | ||
return replacerFromTable(new Map(replacerOption)); | ||
} | ||
|
||
throw 'Incorrect replacer type. It must be a function, a map, or entries.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* eslint-disable no-undef */ | ||
|
||
import { encode } from '../src/encode'; | ||
import { decode } from '../src/decode'; | ||
|
||
describe('replacer', () => { | ||
it('should replace function and symbol via function', () => { | ||
const id = Symbol('id'); | ||
|
||
const source = { | ||
x: 1, | ||
update(value: number) { | ||
this.x += value; | ||
}, | ||
id, | ||
}; | ||
|
||
const buffer = encode(source, { | ||
replacer: (value) => { | ||
if (value === id) return 'id-0'; | ||
if (value === source.update) return 345678; | ||
return value; | ||
}, | ||
}); | ||
|
||
const target = decode(buffer, { | ||
replacer: (value) => { | ||
if (value === 'id-0') return id; | ||
if (value === 345678) return source.update; | ||
return value; | ||
}, | ||
}); | ||
|
||
expect(target).toEqual(source); | ||
}); | ||
|
||
it('should replace function and symbol via map table', () => { | ||
const id = Symbol('id'); | ||
|
||
const source = { | ||
x: 1, | ||
update(value: number) { | ||
this.x += value; | ||
}, | ||
id, | ||
}; | ||
|
||
const buffer = encode(source, { | ||
replacer: new Map<any, any>([ | ||
[id, 'id-0'], | ||
[source.update, 345678], | ||
]), | ||
}); | ||
|
||
const target = decode(buffer, { | ||
replacer: new Map<any, any>([ | ||
['id-0', id], | ||
[345678, source.update], | ||
]), | ||
}); | ||
|
||
expect(target).toEqual(source); | ||
}); | ||
|
||
it('should replace function and symbol via entries', () => { | ||
const id = Symbol('id'); | ||
|
||
const source = { | ||
x: 1, | ||
update(value: number) { | ||
this.x += value; | ||
}, | ||
id, | ||
}; | ||
|
||
const buffer = encode(source, { | ||
replacer: [ | ||
[id, 'id-0'], | ||
[source.update, 345678], | ||
], | ||
}); | ||
|
||
const target = decode(buffer, { | ||
replacer: [ | ||
['id-0', id], | ||
[345678, source.update], | ||
], | ||
}); | ||
|
||
expect(target).toEqual(source); | ||
}); | ||
|
||
it('should throw an error when incorrect replacer', () => { | ||
const source = { x: 1, y: 2 }; | ||
|
||
const buffer = encode(source); | ||
|
||
const act = () => { | ||
decode(buffer, { replacer: 'incorrect replacer' as any as Map<any, any> }); | ||
}; | ||
|
||
expect(act).toThrow('Incorrect replacer type. It must be a function, a map, or entries.'); | ||
}); | ||
}); |