voltace / browser-cookies

Tiny cookies library for the browser
The Unlicense
89 stars 18 forks source link

Format to set the expiry of the cookies #25

Closed MeRahulAhire closed 3 years ago

MeRahulAhire commented 3 years ago

I want to use this library in project but had an doubt regarding setting expiry for the token. Does this library support the ms library format as below?

ms('2 days')  // 172800000
ms('1d')      // 86400000
ms('10h')     // 36000000
ms('2.5 hrs') // 9000000
ms('2h')      // 7200000
ms('1m')      // 60000
ms('5s')      // 5000
ms('1y')      // 31557600000
ms('100')     // 100
ms('-3 days') // -259200000
ms('-1h')     // -3600000
ms('-200')    // -200

or do I need to use it additionally?

hyrious commented 3 years ago

No, there's no built-in support for ms. https://github.com/voltace/browser-cookies/blob/f7de017e295b89241e14d047c2f98f2b69a510c7/src/browser-cookies.js#L18-L23 You can pass a number in days, or any other arguments that new Date() can read. For example:

cookies.set('count', 42, {expires: (new Date().getTime() + ms('1d')) / 864e5});