node-localstorage icon indicating copy to clipboard operation
node-localstorage copied to clipboard

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

Open Hotell opened this issue 7 years ago • 6 comments

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"]

Hotell avatar Mar 03 '17 13:03 Hotell

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 avatar Mar 03 '17 14:03 lmaccherone

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.

lmaccherone avatar Mar 03 '17 14:03 lmaccherone

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.

Hotell avatar Mar 03 '17 15:03 Hotell

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.

piranna avatar Apr 06 '19 11:04 piranna

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

RunDevelopment avatar May 17 '22 15:05 RunDevelopment

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 avatar May 17 '22 15:05 lmaccherone

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

lmaccherone avatar Jul 25 '23 23:07 lmaccherone