Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize Map using a list of pairs #37

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/accessDeep.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { setDeep } from './accessDeep';

describe('setDeep', () => {
it('correctly sets values in maps', () => {
const obj = {
a: new Map([['NaN', 10]]),
};

setDeep(obj, ['a', 0, 0], Number);

expect(obj).toEqual({
a: new Map([[NaN, 10]]),
});
});

it('correctly sets values in sets', () => {
const obj = {
a: new Set(['NaN']),
};

setDeep(obj, ['a', 0], Number);

expect(obj).toEqual({
a: new Set([NaN]),
});
});
});
91 changes: 85 additions & 6 deletions src/accessDeep.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import { isMap, isArray, isPlainObject, isSet } from './is';

export const getNthKey = (value: Map<any, any> | Set<any>, n: number): any => {
const keys = value.keys();
while (n > 0) {
keys.next();
n--;
}

return keys.next().value;
};

export const getDeep = (object: object, path: (string | number)[]): object => {
for (const key of path) {
object = (object as any)[key];
Expand All @@ -7,20 +19,87 @@ export const getDeep = (object: object, path: (string | number)[]): object => {
};

export const setDeep = (
object: object,
object: any,
path: (string | number)[],
mapper: (v: any) => any
): object => {
): any => {
if (path.length === 0) {
return mapper(object);
}

const front = path.slice(0, path.length - 1);
const last = path[path.length - 1];
let parent = object;

const parent: any = getDeep(object, front);
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];

if (isArray(parent)) {
const index = +key;
parent = parent[index];
} else if (isPlainObject(parent)) {
parent = parent[key];
} else if (isSet(parent)) {
const row = +key;
parent = getNthKey(parent, row);
} else if (isMap(parent)) {
const isEnd = i === path.length - 2;
if (isEnd) {
break;
}

const row = +key;
const type = +path[i + 1] === 0 ? 'key' : 'value';

const keyOfRow = getNthKey(parent, row);
switch (type) {
case 'key':
parent = keyOfRow;
break;
case 'value':
parent = parent.get(keyOfRow);
break;
}

i++;
}
}

parent[last] = mapper(parent[last]);
const lastKey = path[path.length - 1];

if (isArray(parent) || isPlainObject(parent)) {
parent[lastKey] = mapper(parent[lastKey]);
}

if (isSet(parent)) {
const oldValue = getNthKey(parent, +lastKey);
const newValue = mapper(oldValue);
if (oldValue !== newValue) {
parent.delete(oldValue);
parent.add(newValue);
}
}

if (isMap(parent)) {
const row = +path[path.length - 2];
const keyToRow = getNthKey(parent, row);

const type = +lastKey === 0 ? 'key' : 'value';
switch (type) {
case 'key': {
const newKey = mapper(keyToRow);
parent.set(newKey, parent.get(keyToRow));

if (newKey !== keyToRow) {
parent.delete(keyToRow);
}
break;
}

case 'value': {
parent.set(keyToRow, mapper(parent.get(keyToRow)));
break;
}
}
}

return object;
};
147 changes: 128 additions & 19 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,20 @@ describe('stringify & parse', () => {
},

output: {
a: {
1: 'a',
NaN: 'b',
},
b: {
2: 'b',
},
d: {
true: 'true key',
},
a: [
[1, 'a'],
['NaN', 'b'],
],
b: [['2', 'b']],
d: [[true, 'true key']],
},

outputAnnotations: {
values: {
a: 'map:number',
b: 'map:string',
d: 'map:boolean',
a: 'map',
'a.1.0': 'number',
b: 'map',
d: 'map',
},
},
},
Expand Down Expand Up @@ -203,11 +200,11 @@ describe('stringify & parse', () => {
a: Number.POSITIVE_INFINITY,
},
output: {
a: undefined,
a: 'Infinity',
},
outputAnnotations: {
values: {
a: 'Infinity',
a: 'number',
},
},
},
Expand All @@ -217,11 +214,11 @@ describe('stringify & parse', () => {
a: Number.NEGATIVE_INFINITY,
},
output: {
a: undefined,
a: '-Infinity',
},
outputAnnotations: {
values: {
a: '-Infinity',
a: 'number',
},
},
},
Expand All @@ -231,11 +228,11 @@ describe('stringify & parse', () => {
a: NaN,
},
output: {
a: undefined,
a: 'NaN',
},
outputAnnotations: {
values: {
a: 'NaN',
a: 'number',
},
},
},
Expand Down Expand Up @@ -274,6 +271,118 @@ describe('stringify & parse', () => {
referentialEqualitiesRoot: ['children.0.parents.0'],
},
},

'works for Maps with two keys that serialize to the same string but have a different reference': {
input: new Map([
[/a/g, 'foo'],
[/a/g, 'bar'],
]),
output: [
['/a/g', 'foo'],
['/a/g', 'bar'],
],
outputAnnotations: {
root: 'map',
values: {
'0.0': 'regexp',
'1.0': 'regexp',
},
},
},

"works for Maps with a key that's referentially equal to another field": {
input: () => {
const robbyBubble = { id: 5 };
const highscores = new Map([[robbyBubble, 5000]]);
return {
highscores,
topScorer: robbyBubble,
} as any;
},
output: {
highscores: [[{ id: 5 }, 5000]],
topScorer: { id: 5 },
},
outputAnnotations: {
values: {
highscores: 'map',
},
referentialEqualities: {
topScorer: ['highscores.0.0'],
},
},
},

'works for referentially equal maps': {
input: () => {
const map = new Map([[1, 1]]);
return {
a: map,
b: map,
};
},
output: {
a: [[1, 1]],
b: [[1, 1]],
},
outputAnnotations: {
values: {
a: 'map',
b: 'map',
},
referentialEqualities: {
a: ['b'],
},
},
customExpectations: value => {
expect(value.a).toBe(value.b);
},
},

'works for maps with non-uniform keys': {
input: {
map: new Map<string | number, number>([
[1, 1],
['1', 1],
]),
},
output: {
map: [
[1, 1],
['1', 1],
],
},
outputAnnotations: {
values: {
map: 'map',
},
},
},

'works for referentially equal values inside a set': {
input: () => {
const user = { id: 2 };
return {
users: new Set([user]),
userOfTheMonth: user,
};
},
output: {
users: [{ id: 2 }],
userOfTheMonth: { id: 2 },
},
outputAnnotations: {
values: {
users: 'set',
},
referentialEqualities: {
userOfTheMonth: ['users.0'],
},
},
customExpectations: value => {
expect(value.users.values().next().value).toBe(value.userOfTheMonth);
},
},
};

function deepFreeze(object: any, alreadySeenObjects = new Set()) {
Expand Down
14 changes: 8 additions & 6 deletions src/plainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('plainer', () => {
});

expect(output).toEqual({
a: {
2: 'hallo',
undefined: null,
},
a: [
[2, 'hallo'],
[undefined, null],
],
b: {
c: [1, 2, /hallo/g],
},
Expand Down Expand Up @@ -60,8 +60,10 @@ describe('plainer', () => {
2 => "hallo",
undefined => null,
},
"a.": null,
"a.2": "hallo",
"a.0.0": 2,
"a.0.1": "hallo",
"a.1.0": undefined,
"a.1.1": null,
"b": Object {
"c": Set {
1,
Expand Down
11 changes: 9 additions & 2 deletions src/plainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ export const plainer = (
);
}

if (isPlainObject(object) || isMap(object)) {
if (isMap(object)) {
return IteratorUtils.map(entries(object), ([key, value], index) => [
plainer(key, walker, [...path, index, 0], alreadySeenObjects),
plainer(value, walker, [...path, index, 1], alreadySeenObjects),
]);
}

if (isPlainObject(object)) {
return Object.fromEntries(
IteratorUtils.map(entries(object), ([key, value]) => [
Object.entries(object).map(([key, value]) => [
key,
plainer(value, walker, [...path, key], alreadySeenObjects),
])
Expand Down
Loading