lmaccherone / node-localstorage

A drop-in substitute for the browser native localStorage API that runs on node.js.
MIT License
450 stars 36 forks source link

Object.keys(localstorage) is not returning valid keys like native LS #27

Closed Hotell closed 1 year ago

Hotell commented 7 years ago

webStorage is a abstracion which uses under the hood localstorage with proper parse/deserialize

   webStorage.set( 'string', 'Hello World' );
    // tslint:disable-next-line:no-magic-numbers
    webStorage.set( 'number', 123 );
    webStorage.set( 'boolean', true );
    webStorage.set( 'object', { foo: 'bar' } );

  it(`should list all storage keys`, () => {

    const actual = webStorage.keys;
    const expected = [
      'string',
      'number',
      'boolean',
      'object',
      'test'
    ];
    expect(actual).toContain(expected);
    expect(Array.isArray(actual)).toEqual(true);

  });

where webStorage.leys ==> Object.keys(window.localStorage)

expect(array).toContain(value)

    Expected array:
      ["_location", "quota", "length", "_bytesInUse", "_keys", "_metaKeyMap", "_eventUrl", "_QUOTA_EXCEEDED_ERR"]
    To contain value:
      ["string", "number", "boolean", "object", "test"]
lmaccherone commented 7 years ago

Let me see if I understand this correctly... When you do localStorage.keys on a real instance of localStorage, you get back the keys you've stored, but when you do it on an instance of node-localstorage, you get back the administrative properties that node-localstorage caches. Correct?

lmaccherone commented 7 years ago

Note, to get the keys from an instance of node-localstorage, you can do localStorage._keys. I know how to make it behave the way I think you want it to behave and I've been meaning to upgrade to support the associative-array dot-property syntax so I'd be willing to work on that more rapidly if you were interested in helping or providing a bounty.

Hotell commented 7 years ago

When you do localStorage.keys on a real instance of localStorage, you get back the keys you've stored, but when you do it on an instance of node-localstorage, you get back the administrative properties that node-localstorage caches. Correct?

yup that's correct.

piranna commented 5 years ago

Two options here, use defineProperties() and set the administrative ones as non enumerable, or use private attributes, that will be available in Node.js 12, that will be the new LTS in just two weeks.

RunDevelopment commented 2 years ago

Why was this closed? This is still an issue, and it breaks the promise of this library:

A drop-in substitute for the browser native localStorage API

lmaccherone commented 2 years ago

I suspect that was before I implemented associative array and dot property syntax. Now that I've done that, this should be much easier. Reopening.

lmaccherone commented 1 year ago

Finally got around to implementing this. If you are still around, let me know what you think @Hotell. It's in v3.0.2