adriancarriger / angularfire2-offline

🔌 A simple wrapper for AngularFire2 to read and write to Firebase while offline, even after a complete refresh.
https://angularfire2-offline.firebaseapp.com/
MIT License
207 stars 48 forks source link

unwrap function requires enumerable: true #71

Open tja4472 opened 7 years ago

tja4472 commented 7 years ago

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.