Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

unwrap function requires enumerable: true #71

Open
tja4472 opened this issue Sep 6, 2017 · 0 comments
Open

unwrap function requires enumerable: true #71

tja4472 opened this issue Sep 6, 2017 · 0 comments

Comments

@tja4472
Copy link

tja4472 commented Sep 6, 2017

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant