angularfire2-offline
angularfire2-offline copied to clipboard
unwrap function requires enumerable: true
database.spec.ts
describe('unwrap & stringify', () => {
it('primitive', () => {
const key = '-KtMoWAYsEtB4qIcroCw';
const value = 'some value';
const expectedResult = '{"$value":"some value","$key":"-KtMoWAYsEtB4qIcroCw"}';
const unwrappedValue = unwrap(key, value, () => true);
const result = stringify(unwrappedValue);
expect(result).toBe(expectedResult);
});
it('object', () => {
const key = '-KtMoWAYsEtB4qIcroCw';
const value = {
description: 'qqqqqqq',
name: 'qqqqq',
};
const expectedResult = '{"description":"qqqqqqq","name":"qqqqq","$key":"-KtMoWAYsEtB4qIcroCw"}' ;
const unwrappedValue = unwrap(key, value, () => true);
const result = stringify(unwrappedValue);
expect(result).toBe(expectedResult);
});
});
database.ts
export function unwrap(key: string, value: any, exists, priority = null) {
let primitive = (/string|number|boolean/).test(typeof value);
let unwrapped = isNil(value) || primitive ? { } : value;
// Change Nil values to null
if (isNil(value)) {
Object.defineProperty(unwrapped, '$value', {
enumerable: true, // <-------
value: null
});
}
let initialValues = { key, value, exists, priority };
return ['value', 'exists', 'key', 'priority'].reduce((p, c) => {
if ((c === 'value' && !primitive ) || isNil(initialValues[c])) { return p; }
Object.defineProperty(p, `$${c}`, {
enumerable: true, // <-------
value: initialValues[c]
});
return p;
}, unwrapped);
}
This may explain the online behaviour of #65.