jmdobry / CacheFactory

CacheFactory is a very simple and useful synchronous key-value store for the browser.
http://www.pseudobry.com/CacheFactory
MIT License
30 stars 18 forks source link

Accept more than number as maxAge (and others) #14

Open jrencz opened 8 years ago

jrencz commented 8 years ago

http://www.w3.org/TR/2011/WD-html5-20110525/timers.html#timers

When the above methods are to get the timeout, they must run the following steps:

  1. Let timeout be the second argument to the method, or zero if the argument was omitted.
  2. Apply the ToString() abstract operation to timeout, and let timeout be the result. [ECMA262] 3. Apply the ToNumber() abstract operation to timeout, and let timeout be the result. [ECMA262]
  3. If timeout is an Infinity value, a Not-a-Number (NaN) value, or negative, let timeout be zero.
  4. Round timeout down to the nearest integer, and let timeout be the result.
  5. Return timeout.

So:

http://www.ecma-international.org/ecma-262/6.0/index.html#sec-tonumber calls http://www.ecma-international.org/ecma-262/6.0/index.html#sec-toprimitive which calls valueOf

Effectively: each data structure, primitive or object that abstract operation ToNumber can cast to a number should be a valid value of all options that now require number

Real life example: http://momentjs.com/docs/#/durations/ Duration implements valueOf and it returns duration length as milliseconds. CacheFactory triggers an error where it shouldn't when duration is passed - browser can handle that.