Skip to content

Instantly share code, notes, and snippets.

@ca057
Last active January 30, 2018 07:03
Show Gist options
  • Save ca057/a7e95c507da1f2d6ccb4c1c777e37ab1 to your computer and use it in GitHub Desktop.
Save ca057/a7e95c507da1f2d6ccb4c1c777e37ab1 to your computer and use it in GitHub Desktop.
Assign __uuid-property to every object ({}) of a given collection.
import { v4 } from 'uuid';
import { map, is } from 'ramda';
const isObject = is(Object);
const isDate = is(Date);
const isRegExp = is(RegExp);
export const assignIds = data => {
if (Array.isArray(data)) {
return data.map(assignIds);
}
// not serializable, but interesting edge cases
if (isObject(data) && !isDate(data) && !isRegExp(data)) {
return {
...map(value => {
if (Array.isArray(value) || isObject(value)) {
return assignIds(value);
}
return value;
}, data),
__uuid: v4(),
};
}
return data;
};
import { assignIds } from './assignIds';
jest.mock('uuid', () => ({
v4: () => 'UUID',
}));
describe('assignIds', () => {
it('should assign an id to a single object', () => {
const input1d = {
foo: 'bar',
};
const expected1d = {
foo: 'bar',
__uuid: 'UUID',
};
expect(assignIds(input1d)).toMatchObject(expected1d);
});
it('should assign an id to every item in the array', () => {
const input = [{ foo: 'bar' }, { foo: 'bar' }];
const expected = [
{ foo: 'bar', __uuid: 'UUID' },
{ foo: 'bar', __uuid: 'UUID' },
];
expect(assignIds(input)).toMatchObject(expected);
});
it('should assign an id to every nested array in an array', () => {
const input = [
[{ foo: 'bar' }, { foo: 'bar' }],
[{ foo: 'bar' }, { foo: 'bar' }],
];
const expected = [
[{ foo: 'bar', __uuid: 'UUID' }, { foo: 'bar', __uuid: 'UUID' }],
[{ foo: 'bar', __uuid: 'UUID' }, { foo: 'bar', __uuid: 'UUID' }],
];
expect(assignIds(input)).toMatchObject(expected);
});
it('should assign an id to nested objects', () => {
const input = {
foo: 'bar',
bar: {
nestedFoo: 'nestedBar',
},
};
const expected = {
__uuid: 'UUID',
foo: 'bar',
bar: {
__uuid: 'UUID',
nestedFoo: 'nestedBar',
},
};
expect(assignIds(input)).toMatchObject(expected);
});
it('should deal with complex sample', () => {
const input = {
a: 'this is something',
b: {
c: {
d: {
e: 'this is something',
f: [
'this is something',
12345,
new Date(),
{
g: 'this is something',
},
],
},
},
},
h: [
null,
0,
undefined,
{
i: {
j: {
l: 'this is something',
m: new RegExp('lol'),
n: true,
},
},
},
],
};
const expected = {
__uuid: 'UUID',
a: 'this is something',
b: {
__uuid: 'UUID',
c: {
__uuid: 'UUID',
d: {
__uuid: 'UUID',
e: 'this is something',
f: [
'this is something',
12345,
new Date(),
{
__uuid: 'UUID',
g: 'this is something',
},
],
},
},
},
h: [
null,
0,
undefined,
{
__uuid: 'UUID',
i: {
__uuid: 'UUID',
j: {
__uuid: 'UUID',
l: 'this is something',
m: new RegExp('lol'),
n: true,
},
},
},
],
};
expect(assignIds(input)).toMatchObject(expected);
});
it('should not assign ids to primitive values or collections including those', () => {
const array = [1, 2, 3];
expect(assignIds(array)).toEqual(array);
expect(assignIds(42)).toEqual(42);
expect(assignIds('42')).toEqual('42');
expect(assignIds(true)).toEqual(true);
expect(assignIds(false)).toEqual(false);
});
it('should return falsy values as they are', () => {
expect(assignIds(undefined)).toEqual(undefined);
expect(assignIds(null)).toEqual(null);
expect(assignIds(NaN)).toEqual(NaN);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment