sindresorhus/electron-store

Do you want to work on this issue?

You can request for a bounty in order to promote it!


1 active bounty request

$40.00
Solve someone else's bounty request by clicking on it and completing the funding to earn additional credits

Support dates #18

sindresorhus posted onGitHub

Would be nice if you could store.set('foo', new Date()) and get back a Date object when you do store.get('foo'). Could probably make use of the second argument to JSON.parse(). We need to save some kind of information to indicate that it's a native Date format. Maybe a separate __meta__ key or something. Suggestions welcome.


You could serialize dates to ISO 8601 format in .set(). Then you could parse ISO 8601 dates into Date objects in .get() but override their .toString() to return the string as it was stored (rather than the default (new Date()).toString() funkiness).

That would:

  • Avoid adding extra fields
  • Be convenient, regardless of how the date was set (either as a string literal or a Date object)
  • Be safe, in case the date was set as a string literal
posted by sholladay almost 8 years ago

Then you could parse ISO 8601 dates into Date objects in .get() but override their .toString() to return the string as it was stored (rather than the default (new Date()).toString() funkiness).

That sounds hackish. What if the user tries to do typeof store.get('date-string') and expected it to be string, not Date. I would rather store some extra metadata than having unexpected behavior.

posted by sindresorhus almost 8 years ago

Just another suggestion:

Rather than a separate __meta__ key, what are your thoughts on prepending the date type to the value? Meaning:

const d = new Date();
const s = d.toString();

// Current:
conf.set('date', d);    // { "date": "Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }
conf.set('dateStr', s); // { "date": "Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }

// Suggested:
conf.set('date', d);    // { "date": "__date__,Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }
conf.set('dateStr', s); // { "date": "Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }

If the value passed to .set() is an instance of Date we could prepend a special string to the serialised string, and just check for that when parsing it back from JSON.

posted by acheronfail about 7 years ago

@acheronfail Can't do that. What if the user actually sets __date__,Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT) themselves and expects a string back.

posted by sindresorhus about 7 years ago

conf (which is where this should be first implemented) now has a __internal__ that we could use to store this kind of metadata.

posted by sindresorhus over 5 years ago

@issuehunt has funded $80.00 to this issue.


posted by issuehunt-app[bot] over 5 years ago

How about adding the possibility to store a function in the store and execute it automatically?

e.g. conf.set('foo', () => new Date());

And whenever the user fetches the value, we just evaluate if it's a function or not and run it.

posted by rafaelramalho19 over 5 years ago

@rafaelramalho19 I don't think that's a good idea. That would be a security nightmare. Anything on the computer could add code to the plaintext config file and have the app execute it in a trusted context.

posted by sindresorhus over 5 years ago

I guess thats true. Just wanted it to allow for broader use-cases.

posted by rafaelramalho19 over 5 years ago

👋 I stumbled upon this library:

https://github.com/wynntee/joss

Would it help in this case?

posted by dnlup about 4 years ago
posted by Richienb about 4 years ago

v8.serialize?

Interesting, you mean serializing just the Date or the whole db?

posted by dnlup about 4 years ago

By serializing the whole db, there is no longer a problem with creating non-conflicting values.

posted by Richienb about 4 years ago

How about just using timestamps ?

posted by KaKi87 over 3 years ago

How about just using timestamps ?

The challenge would then become telling apart a timestamp from a regular number

posted by Richienb over 3 years ago

Fund this Issue

$80.00
Funded
Only logged in users can fund an issue

Pull requests

Recent activities

richienb submitted an output to  sindresorhus/ electron-store# 18
over 3 years ago
richienb withdrew the output  to  sindresorhus/ electron-store# 18
over 3 years ago