pax_global_header00006660000000000000000000000064137760452730014530gustar00rootroot0000000000000052 comment=cd7cc2738c183fd00f7e8bf5ba6a96935f2b3dab memoizee-0.4.15/000077500000000000000000000000001377604527300134315ustar00rootroot00000000000000memoizee-0.4.15/.editorconfig000066400000000000000000000004201377604527300161020ustar00rootroot00000000000000# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true # Unix-style newlines with a newline ending every file [*] end_of_line = lf insert_final_newline = true indent_style = tab [{*.json,*.yml}] indent_style = space indent_size = 2 memoizee-0.4.15/.github/000077500000000000000000000000001377604527300147715ustar00rootroot00000000000000memoizee-0.4.15/.github/FUNDING.yml000066400000000000000000000000311377604527300166000ustar00rootroot00000000000000tidelift: "npm/memoizee" memoizee-0.4.15/.gitignore000066400000000000000000000000571377604527300154230ustar00rootroot00000000000000/node_modules npm-debug.log /package-lock.json memoizee-0.4.15/.npmignore000066400000000000000000000000661377604527300154320ustar00rootroot00000000000000/.editorconfig /.github /.testignore /test /benchmark memoizee-0.4.15/.testignore000066400000000000000000000000131377604527300156070ustar00rootroot00000000000000/benchmark memoizee-0.4.15/CHANGELOG.md000066400000000000000000000052531377604527300152470ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. ### [0.4.15](https://github.com/medikoo/memoizee/compare/v0.4.14...v0.4.15) (2021-01-08) _Maintainance update_ ## [0.4.14](https://github.com/medikoo/memoizee/compare/v0.4.13...v0.4.14) (2018-08-13) ### Bug Fixes - ensure to not force unhandled rejections ([9b416ea](https://github.com/medikoo/memoizee/commit/9b416ea)) ## [0.4.13](https://github.com/medikoo/memoizee/compare/v0.4.12...v0.4.13) (2018-08-06) ### Features - **promise:** support cancellation case ([b4b018d](https://github.com/medikoo/memoizee/commit/b4b018d)), closes [#97](https://github.com/medikoo/memoizee/issues/97) ## [0.4.12](https://github.com/medikoo/memoizee/compare/v0.4.11...v0.4.12) (2018-02-23) ### Bug Fixes - **max-age:** unref timeouts to not block processes from exiting ([5bcc5a4](https://github.com/medikoo/memoizee/commit/5bcc5a4)), closes [#25](https://github.com/medikoo/memoizee/issues/25) ## [0.4.11](https://github.com/medikoo/memoizee/compare/v0.4.10...v0.4.11) (2017-09-11) ### Bug Fixes - \_get and \_has internal args handling. ([7cb1c7a](https://github.com/medikoo/memoizee/commit/7cb1c7a)), closes [#88](https://github.com/medikoo/memoizee/issues/88) ## [0.4.10](https://github.com/medikoo/memoizee/compare/v0.4.9...v0.4.10) (2017-09-07) ### Bug Fixes - remove then:finally mode as it can't work right ([5b79698](https://github.com/medikoo/memoizee/commit/5b79698)) ## [0.4.9](https://github.com/medikoo/memoizee/compare/v0.4.8...v0.4.9) (2017-08-29) ## [0.4.8](https://github.com/medikoo/memoizee/compare/v0.4.7...v0.4.8) (2017-08-29) ## [0.4.7](https://github.com/medikoo/memoizee/compare/v0.4.6...v0.4.7) (2017-08-29) ### Features - improve 'promise' mode handling ([759e315](https://github.com/medikoo/memoizee/commit/759e315)) - improve internal promise validation ([d23b94f](https://github.com/medikoo/memoizee/commit/d23b94f)) ## [0.4.6](https://github.com/medikoo/memoizee/compare/v0.4.5...v0.4.6) (2017-08-24) - `profileName` option for naming memoizee instances in profile output ## [0.4.5](https://github.com/medikoo/memoizee/compare/v0.4.4...v0.4.5) (2017-05-10) ### Bug Fixes - resolution of extensions with weak handling ([f29a97b](https://github.com/medikoo/memoizee/commit/f29a97b)), closes [#79](https://github.com/medikoo/memoizee/issues/79) ## Old changelog See `CHANGES` memoizee-0.4.15/CHANGES000066400000000000000000000110541377604527300144250ustar00rootroot00000000000000-- For new changelog see CHANGELOG.md v0.4.4 -- 2017.03.15 * Expose _get and _has methods. It provides temporary means for better cache inspection until v1 is delivered v0.4.3 -- 2017.02.20 * Fix normalization resolver, so it validates already normalized resolvers (reported at #70) v0.4.2 -- 2017.02.17 * Guard proper promise resolution in promise mode * Improve documentation * Improve npm scripts configuration * Improve CI configuration v0.4.1 -- 2016.07.13 * Fix promise mode to not use `then` and `finally` pair. Thanks @Kovensky * Add 'done' mode to 'promise' mode (so now callbacks can be registered through `done` but no `finally` event if it's implemented) v0.4.0 -- 2016.07.07 * Introduce 'promise' mode * Change signature of internal events for 'async' case * Document 'normalizer' option v0.3.10 -- 2016.04.21 * Fix resolvers bug (#12) * Fix arguments support for weak mode * Update repository address after rename v0.3.9 -- 2015.08.04 * Update dependencies * Improve documentation * FIx spelling of LICENSE v0.3.8 -- 2014.10.08 * Introduce WeakMap based `weak` mode v0.3.7 -- 2014.08.14 * Fix prefetch support for asynchronous functions (#19) * Configure lint scripts v0.3.6 -- 2014.07.28 * Fix race condition issue related to clear/delete calls and returning id's (#18) * Fix maxAge major cache handling issue (on timeout instead of individual record whole cache was cleared) v0.3.5 -- 2014.07.07 * Fix internal id genaration, for primitive, length = 1 case. Ids were not serialized to strings, and that caused issues with other internal logiv, which e.g. treated `null` case specifically. Fixes #15 (Thanks @isaacg for reporting) v0.3.4 -- 2014.06.22 * Fix async handling in case of clear between two async calls (assurance of unique cache ids solves that). Fixes #13 v0.3.3 -- 2014.05.12 * Fix profiler special property definition v0.3.2 -- 2014.05.01 * Provide no-arguments memoization out of a box (no need to provide normalizer to `memoizee/plain`) v0.3.1 -- 2014.04.27 * Update package.json to use latest 'tad' engine (fixes tests evaluation) v0.3.0 -- 2014.04.27 Major reorganization and partial refactoring * Move out main modules from lib folder * Introduce normalizer based memoization. Primitive and regular handlers were converted into dedicated normalizers (located in lib/normalizers folder). Custom normalizers can be provided at run time via `normalizer` option. * Provide 'plain' module which does not import (require) any extensions or normalizers. Thanks to that it's lighter for e.g. client-side bundle. Any extensions that are expected to be used should be required upfront, normalizers should be provided via `normalizer` option. * Rename `memoized.clear` into `memoized.delete`, and `memoized.clearAll` into `memoized.clear` * Rename `memoized.clearRef` into `memoized.deleteRef` (applies to 'refCounter' option) * Remove 'method' option. Instead 'methods' and 'methods-plan' modules are provided which generate descriptors for lazy created memoized methods. * 'profile' is no longer an extension. It's provided as dedicated module. * Clean up logic for `async` handling * Take out 'max' extension's LRU logic into external `lru-queue` package. See https://github.com/medikoo/lru-queue * Remove possibility to access original arguments when resolvers are used * Assure expected length of memoized functions * Remove Makefile (it's environment agnostic package) v0.2.6 -- 2013.10.08 * Fix internal events propagation when handling async calls that resolve with errors. `asyncpurge` was emitted for values that had no `asyncinit` emitted. Issue #9 v0.2.5 -- 2013.06.21 * Fix primitive handling for dynamic arguments length v0.2.4 -- 2013.03.23 * Throw on circular invocations, they cannot be memoized as intended. v0.2.3 -- 2012.10.04 * Fixed serious bug related to not properly cleared cache when working in regular mode v0.2.2 -- 2012.10.03 * preFetch functionality for maxAge variant * Prevent memoization of already memoized functions v0.2.1 -- 2012.09.21 * Fix missing global reference in method option logic * Fix variable visibility in async option logic * Lint cleanup v0.2.0 -- 2012.09.21 * Modularization and general algorithm cleanup * Cache is limited (max option) using LRU instead of FIFO algorithm * Improved async mode, and its handling by other options v0.1.1 -- 2012.09.19 * Fix dispose invocations for no arguments call * Small documentation improvements v0.1.0 -- 2012.09.18 * Initial. Derived from es5-ext package and added `async`, `maxAge`, `refCounter`, `max` and `dispose` options. memoizee-0.4.15/LICENSE000066400000000000000000000014051377604527300144360ustar00rootroot00000000000000ISC License Copyright (c) 2012-2018, Mariusz Nowak, @medikoo, medikoo.com Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. memoizee-0.4.15/README.md000066400000000000000000000426541377604527300147230ustar00rootroot00000000000000[![*nix build status][nix-build-image]][nix-build-url] [![Windows build status][win-build-image]][win-build-url] ![Transpilation status][transpilation-image] [![npm version][npm-image]][npm-url] # Memoizee ## Complete memoize/cache solution for JavaScript _Originally derived from [es5-ext](https://github.com/medikoo/es5-ext) package._ Memoization is best technique to save on memory or CPU cycles when we deal with repeated operations. For detailed insight see: http://en.wikipedia.org/wiki/Memoization ### Features * Works with any type of function arguments – **no serialization is needed** * Works with [**any length of function arguments**](#arguments-length). Length can be set as fixed or dynamic. * One of the [**fastest**](#benchmarks) available solutions. * Support for [**promises**](#promise-returning-functions) and [**asynchronous functions**](#nodejs-callback-style-functions) * [**Primitive mode**](#primitive-mode) which assures fast performance when arguments are convertible to strings. * [**WeakMap based mode**](#weakmap-based-configurations) for garbage collection friendly configuration * Can be configured [**for methods**](#memoizing-methods) (when `this` counts in) * Cache [**can be cleared manually**](#manual-clean-up) or [**after specified timeout**](#expire-cache-after-given-period-of-time) * Cache size can be **[limited on LRU basis](#limiting-cache-size)** * Optionally [**accepts resolvers**](#argument-resolvers) that normalize function arguments before passing them to underlying function. * Optional [**reference counter mode**](#reference-counter), that allows more sophisticated cache management * [**Profile tool**](#profiling--statistics) that provides valuable usage statistics * Covered by [**over 500 unit tests**](#tests) ### Installation In your project path — **note the two `e`'s in `memoizee`:** $ npm install memoizee _`memoize` name was already taken, therefore project is published as `memoizee` on NPM._ To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/) ### Usage ```javascript var memoize = require("memoizee"); var fn = function(one, two, three) { /* ... */ }; memoized = memoize(fn); memoized("foo", 3, "bar"); memoized("foo", 3, "bar"); // Cache hit ``` __Note__: Invocations that throw exceptions are not cached. ### Configuration All below options can be applied in any combination #### Arguments length By default fixed number of arguments that function take is assumed (it's read from function's `length` property) this can be overridden: ```javascript memoized = memoize(fn, { length: 2 }); memoized("foo"); // Assumed: 'foo', undefined memoized("foo", undefined); // Cache hit memoized("foo", 3, {}); // Third argument is ignored (but passed to underlying function) memoized("foo", 3, 13); // Cache hit ``` __Note:__ [Parameters predefined with default values (ES2015+ feature)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) are not reflected in function's `length`, therefore if you want to memoize them as well, you need to tweak `length` setting accordingly Dynamic _length_ behavior can be forced by setting _length_ to `false`, that means memoize will work with any number of arguments. ```javascript memoized = memoize(fn, { length: false }); memoized("foo"); memoized("foo"); // Cache hit memoized("foo", undefined); memoized("foo", undefined); // Cache hit memoized("foo", 3, {}); memoized("foo", 3, 13); memoized("foo", 3, 13); // Cache hit ``` #### Primitive mode If we work with large result sets, or memoize hot functions, default mode may not perform as fast as we expect. In that case it's good to run memoization in _primitive_ mode. To provide fast access, results are saved in hash instead of an array. Generated hash ids are result of arguments to string conversion. **Mind that this mode will work correctly only if stringified arguments produce unique strings.** ```javascript memoized = memoize(fn, { primitive: true }); memoized("/path/one"); memoized("/path/one"); // Cache hit ``` #### Cache id resolution (normalization) By default cache id for given call is resolved either by: * Direct Comparison of values passed in arguments as they are. In such case two different objects, even if their characteristics is exactly same (e.g. `var a = { foo: 'bar' }, b = { foo: 'bar' }`) will be treated as two different values. * Comparison of stringified values of given arguments (`primitive` mode), which serves well, when arguments are expected to be primitive values, or objects that stringify naturally do unique values (e.g. arrays) Still above two methods do not serve all cases, e.g. if we want to memoize function where arguments are hash objects which we do not want to compare by instance but by its content. ##### Writing custom cache id normalizers There's a `normalizer` option through which we can pass custom cache id normalization function e.g. if we want to memoize a function where argument is a hash object which we do not want to compare by instance but by its content, then we can achieve it as following: ```javascript var mfn = memoize( function(hash) { // body of memoized function }, { normalizer: function(args) { // args is arguments object as accessible in memoized function return JSON.stringify(args[0]); } } ); mfn({ foo: "bar" }); mfn({ foo: "bar" }); // Cache hit ``` #### Argument resolvers When we're expecting arguments of certain type it's good to coerce them before doing memoization. We can do that by passing additional resolvers array: ```javascript memoized = memoize(fn, { length: 2, resolvers: [String, Boolean] }); memoized(12, [1, 2, 3].length); memoized("12", true); // Cache hit memoized( { toString: function() { return "12"; } }, {} ); // Cache hit ``` **Note. If your arguments are collections (arrays or hashes) that you want to memoize by content (not by self objects), you need to cast them to strings**, for it's best to just use [primitive mode](#primitive-mode). Arrays have standard string representation and work with primitive mode out of a box, for hashes you need to define `toString` method, that will produce unique string descriptions, or rely on `JSON.stringify`. Similarly **if you want to memoize functions by their code representation not by their objects, you should use primitive mode**. #### Memoizing asynchronous functions ##### Promise returning functions With _promise_ option we indicate that we memoize a function that returns promise. The difference from natural behavior is that in case when promise was rejected with exception, the result is immediately removed from memoize cache, and not kept as further reusable result. ```javascript var afn = function(a, b) { return new Promise(function(res) { res(a + b); }); }; memoized = memoize(afn, { promise: true }); memoized(3, 7); memoized(3, 7); // Cache hit ``` ###### Important notice on internal promises handling Default handling stands purely on _then_ which has side-effect of muting eventual unhandled rejection notifications. Alternatively we can other (explained below), by stating with `promise` option desired mode: ```javascript memoized = memoize(afn, { promise: "done:finally" }); ``` Supported modes * `then` _(default)_. Values are resolved purely by passing callbacks to `promise.then`. **Side effect is that eventual unhandled rejection on given promise come with no logged warning!**, and that to avoid implied error swallowing both states are resolved tick after callbacks were invoked * `done` Values are resolved purely by passing callback to `done` method. **Side effect is that eventual unhandled rejection on given promise come with no logged warning!**. * `done:finally` The only method that may work with no side-effects assuming that promise implementaion does not throw unconditionally if no _onFailure_ callback was passed to `done`, and promise error was handled by other consumer (this is not commonly implemented _done_ behavior). Otherwise side-effect is that exception is thrown on promise rejection (highly not recommended) ##### Node.js callback style functions With _async_ option we indicate that we memoize asynchronous (Node.js style) function Operations that result with an error are not cached. ```javascript afn = function(a, b, cb) { setTimeout(function() { cb(null, a + b); }, 200); }; memoized = memoize(afn, { async: true }); memoized(3, 7, function(err, res) { memoized(3, 7, function(err, res) { // Cache hit }); }); memoized(3, 7, function(err, res) { // Cache hit }); ``` #### Memoizing methods When we are defining a prototype, we may want to define a method that will memoize it's results in relation to each instance. A basic way to obtain that would be: ```javascript var Foo = function() { this.bar = memoize(this.bar.bind(this), { someOption: true }); // ... constructor logic }; Foo.prototype.bar = function() { // ... method logic }; ``` There's a lazy methods descriptor generator provided: ```javascript var d = require("d"); var memoizeMethods = require("memoizee/methods"); var Foo = function() { // ... constructor logic }; Object.defineProperties( Foo.prototype, memoizeMethods({ bar: d( function() { // ... method logic }, { someOption: true } ) }) ); ``` #### WeakMap based configurations In this case memoization cache is not bound to memoized function (which we may want to keep forever), but to objects for which given results were generated. This mode works only for functions of which first argument is expected to be an object. It can be combined with other options mentioned across documentation. However due to WeakMap specificity global clear is not possible. ```javascript var memoize = require("memoizee/weak"); var memoized = memoize(function(obj) { return Object.keys(obj); }); var obj = { foo: true, bar: false }; memoized(obj); memoized(obj); // Cache hit ``` #### Cache handling ##### Manual clean up: Delete data for particular call. ```javascript memoized.delete("foo", true); ``` Arguments passed to `delete` are treated with same rules as input arguments passed to function Clear all cached data: ```javascript memoized.clear(); ``` ##### Expire cache after given period of time With _maxAge_ option we can ensure that cache for given call is cleared after predefined period of time (in milliseconds) ```javascript memoized = memoize(fn, { maxAge: 1000 }); // 1 second memoized("foo", 3); memoized("foo", 3); // Cache hit setTimeout(function() { memoized("foo", 3); // No longer in cache, re-executed memoized("foo", 3); // Cache hit }, 2000); ``` Additionally we may ask to _pre-fetch_ in a background a value that is about to expire. _Pre-fetch_ is invoked only if value is accessed close to its expiry date. By default it needs to be within at least 33% of _maxAge_ timespan before expire: ```javascript memoized = memoize(fn, { maxAge: 1000, preFetch: true }); // Defaults to 0.33 memoized("foo", 3); memoized("foo", 3); // Cache hit setTimeout(function() { memoized("foo", 3); // Cache hit }, 500); setTimeout(function() { memoized("foo", 3); // Cache hit, silently pre-fetched in next tick }, 800); setTimeout(function() { memoized("foo", 3); // Cache hit }, 1300); ``` _Pre-fetch_ timespan can be customized: ```javascript memoized = memoize(fn, { maxAge: 1000, preFetch: 0.6 }); memoized("foo", 3); memoized("foo", 3); // Cache hit setTimeout(function() { memoized("foo", 3); // Cache hit, silently pre-fetched in next tick }, 500); setTimeout(function() { memoized("foo", 3); // Cache hit }, 1300); ``` _Thanks [@puzrin](https://github.com/puzrin) for helpful suggestions concerning this functionality_ ##### Reference counter We can track number of references returned from cache, and manually delete them. When the last reference is cleared, the cache is purged automatically: ```javascript memoized = memoize(fn, { refCounter: true }); memoized("foo", 3); // refs: 1 memoized("foo", 3); // Cache hit, refs: 2 memoized("foo", 3); // Cache hit, refs: 3 memoized.deleteRef("foo", 3); // refs: 2 memoized.deleteRef("foo", 3); // refs: 1 memoized.deleteRef("foo", 3); // refs: 0, Cache purged for 'foo', 3 memoized("foo", 3); // Re-executed, refs: 1 ``` ##### Limiting cache size With _max_ option you can limit cache size, it's backed with [LRU algorithm](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used), provided by low-level [lru-queue](https://github.com/medikoo/lru-queue) utility. The _size_ relates purely to count of results we want to keep in cache, it doesn't relate to memory cost associated with cache value (but such feature is likely to be introduced with next version of memoizee). ```javascript memoized = memoize(fn, { max: 2 }); memoized("foo", 3); memoized("bar", 7); memoized("foo", 3); // Cache hit memoized("bar", 7); // Cache hit memoized("lorem", 11); // Cache cleared for 'foo', 3 memoized("bar", 7); // Cache hit memoized("foo", 3); // Re-executed, Cache cleared for 'lorem', 11 memoized("lorem", 11); // Re-executed, Cache cleared for 'bar', 7 memoized("foo", 3); // Cache hit memoized("bar", 7); // Re-executed, Cache cleared for 'lorem', 11 ``` ##### Registering dispose callback You can register a callback to be called on each value removed from the cache: ```javascript memoized = memoize(fn, { dispose: function(value) { /*…*/ } }); var foo3 = memoized("foo", 3); var bar7 = memoized("bar", 7); memoized.clear("foo", 3); // Dispose called with foo3 value memoized.clear("bar", 7); // Dispose called with bar7 value ``` ### Benchmarks Simple benchmark tests can be found in _benchmark_ folder. Currently it's just plain simple calculation of fibonacci sequences. To run it you need to install other test candidates: $ npm install underscore lodash lru-cache secondary-cache Example output taken under Node v0.10.35 on 2011 MBP Pro: ``` Fibonacci 3000 x10: 1: 15ms Memoizee (primitive mode) 2: 15ms Underscore 3: 18ms lru-cache LRU (max: 1000) 4: 21ms secondary-cache LRU (max: 1000) 5: 37ms Lo-dash 6: 62ms Memoizee (primitive mode) LRU (max: 1000) 7: 163ms Memoizee (object mode) LRU (max: 1000) 8: 195ms Memoizee (object mode) ``` ### Profiling & Statistics If you want to make sure how much you benefit from memoization or just check if memoization works as expected, loading profile module will give access to all valuable information. **Module needs to be imported before any memoization (that we want to track) is configured. Mind also that running profile module affects performance, it's best not to use it in production environment** ```javascript var memProfile = require('memoizee/profile'); ... ... memoize(fn); ... memoize(fn, { profileName: 'Some Function' }) ... memoize(fn, { profileName: 'Another Function' }) ``` Access statistics at any time: ```javascript memProfile.statistics; // Statistics accessible for programmatic use console.log(memProfile.log()); // Output statistics data in readable form ``` Example console output: ``` ------------------------------------------------------------ Memoize statistics: Init Cache %Cache Source location 11604 35682 75.46 (all) 2112 19901 90.41 Some Function, at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:276:12 2108 9087 81.17 Another Function, at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:293:10 6687 2772 29.31 at /Users/medikoo/Projects/_packages/next/lib/fs/watch.js:125:9 697 3922 84.91 at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:277:15 ------------------------------------------------------------ ``` * _Init_ – Initial hits * _Cache_ – Cache hits * _%Cache_ – What's the percentage of cache hits (of all function calls) * _Source location_ – Where in the source code given memoization was initialized ### Tests $ npm test Project cross-browser compatibility to be supported by: ## Security contact information To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. ---
Get professional support for d with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
### Contributors * [@puzrin](https://github.com/puzrin) (Vitaly Puzrin) * Proposal and help with coining right _pre-fetch_ logic for [_maxAge_](https://github.com/medikoo/memoize#expire-cache-after-given-period-of-time) variant [nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/memoizee/branches/master/shields_badge.svg [nix-build-url]: https://semaphoreci.com/medikoo-org/memoizee [win-build-image]: https://ci.appveyor.com/api/projects/status/hsxubnbwe89c26bu?svg=true [win-build-url]: https://ci.appveyor.com/project/medikoo/memoizee [transpilation-image]: https://img.shields.io/badge/transpilation-free-brightgreen.svg [npm-image]: https://img.shields.io/npm/v/memoizee.svg [npm-url]: https://www.npmjs.com/package/memoizee memoizee-0.4.15/benchmark/000077500000000000000000000000001377604527300153635ustar00rootroot00000000000000memoizee-0.4.15/benchmark/fibonacci.js000066400000000000000000000053041377604527300176400ustar00rootroot00000000000000/* global console */ /* eslint no-console: 0, id-length: 0 */ "use strict"; // Simple benchmark for very simple memoization case (fibonacci series) // To run it, do following in memoizee package path: // // $ npm install underscore lodash lru-cache secondary-cache // $ node benchmark/fibonacci.js var forEach = require("es5-ext/object/for-each") , pad = require("es5-ext/string/#/pad") , memoizee = require("..") , underscore = require("underscore").memoize , lodash = require("lodash").memoize , lruCache = require("lru-cache") , lruSecondary = require("secondary-cache/lib/lru-cache"); var now = Date.now , time , getFib , lru , memo , total , index = 3000 , count = 10 , i , lruMax = 1000 , data = {} , lruObj; getFib = function (memoize, opts) { var fib = memoize(function (x) { return x < 2 ? 1 : fib(x - 1) + fib(x - 2); }, opts); return fib; }; lru = function (x) { var value = lruObj.get(x); if (value === undefined) { value = x < 2 ? 1 : lru(x - 1) + lru(x - 2); lruObj.set(x, value); } return value; }; console.log("Fibonacci", index, "x" + count + ":\n"); total = 0; i = count; while (i--) { memo = getFib(memoizee); time = now(); memo(index); total += now() - time; } data["Memoizee (object mode)"] = total; total = 0; i = count; while (i--) { memo = getFib(memoizee, { primitive: true }); time = now(); memo(index); total += now() - time; } data["Memoizee (primitive mode)"] = total; total = 0; i = count; while (i--) { memo = getFib(underscore); time = now(); memo(index); total += now() - time; } data.Underscore = total; total = 0; i = count; while (i--) { memo = getFib(lodash); time = now(); memo(index); total += now() - time; } data["Lo-dash"] = total; total = 0; i = count; while (i--) { memo = getFib(memoizee, { primitive: true, max: lruMax }); time = now(); memo(index); total += now() - time; } data["Memoizee (primitive mode) LRU (max: 1000)"] = total; total = 0; i = count; while (i--) { memo = getFib(memoizee, { max: lruMax }); time = now(); memo(index); total += now() - time; } data["Memoizee (object mode) LRU (max: 1000)"] = total; total = 0; i = count; while (i--) { lruObj = lruCache({ max: lruMax }); time = now(); lru(index); total += now() - time; } data["lru-cache LRU (max: 1000)"] = total; total = 0; i = count; while (i--) { lruObj = lruSecondary(lruMax); time = now(); lru(index); total += now() - time; } data["secondary-cache LRU (max: 1000)"] = total; forEach( data, function (value, name, obj, currentIndex) { console.log(currentIndex + 1 + ":", pad.call(value, " ", 5) + "ms ", name); }, null, function (a, b) { return this[a] - this[b]; } ); memoizee-0.4.15/ext/000077500000000000000000000000001377604527300142315ustar00rootroot00000000000000memoizee-0.4.15/ext/async.js000066400000000000000000000104071377604527300157060ustar00rootroot00000000000000/* eslint consistent-this: 0, no-shadow:0, no-eq-null: 0, eqeqeq: 0, no-unused-vars: 0 */ // Support for asynchronous functions "use strict"; var aFrom = require("es5-ext/array/from") , objectMap = require("es5-ext/object/map") , mixin = require("es5-ext/object/mixin") , defineLength = require("es5-ext/function/_define-length") , nextTick = require("next-tick"); var slice = Array.prototype.slice, apply = Function.prototype.apply, create = Object.create; require("../lib/registered-extensions").async = function (tbi, conf) { var waiting = create(null) , cache = create(null) , base = conf.memoized , original = conf.original , currentCallback , currentContext , currentArgs; // Initial conf.memoized = defineLength(function (arg) { var args = arguments, last = args[args.length - 1]; if (typeof last === "function") { currentCallback = last; args = slice.call(args, 0, -1); } return base.apply(currentContext = this, currentArgs = args); }, base); try { mixin(conf.memoized, base); } catch (ignore) {} // From cache (sync) conf.on("get", function (id) { var cb, context, args; if (!currentCallback) return; // Unresolved if (waiting[id]) { if (typeof waiting[id] === "function") waiting[id] = [waiting[id], currentCallback]; else waiting[id].push(currentCallback); currentCallback = null; return; } // Resolved, assure next tick invocation cb = currentCallback; context = currentContext; args = currentArgs; currentCallback = currentContext = currentArgs = null; nextTick(function () { var data; if (hasOwnProperty.call(cache, id)) { data = cache[id]; conf.emit("getasync", id, args, context); apply.call(cb, data.context, data.args); } else { // Purged in a meantime, we shouldn't rely on cached value, recall currentCallback = cb; currentContext = context; currentArgs = args; base.apply(context, args); } }); }); // Not from cache conf.original = function () { var args, cb, origCb, result; if (!currentCallback) return apply.call(original, this, arguments); args = aFrom(arguments); cb = function self(err) { var cb, args, id = self.id; if (id == null) { // Shouldn't happen, means async callback was called sync way nextTick(apply.bind(self, this, arguments)); return undefined; } delete self.id; cb = waiting[id]; delete waiting[id]; if (!cb) { // Already processed, // outcome of race condition: asyncFn(1, cb), asyncFn.clear(), asyncFn(1, cb) return undefined; } args = aFrom(arguments); if (conf.has(id)) { if (err) { conf.delete(id); } else { cache[id] = { context: this, args: args }; conf.emit("setasync", id, typeof cb === "function" ? 1 : cb.length); } } if (typeof cb === "function") { result = apply.call(cb, this, args); } else { cb.forEach(function (cb) { result = apply.call(cb, this, args); }, this); } return result; }; origCb = currentCallback; currentCallback = currentContext = currentArgs = null; args.push(cb); result = apply.call(original, this, args); cb.cb = origCb; currentCallback = cb; return result; }; // After not from cache call conf.on("set", function (id) { if (!currentCallback) { conf.delete(id); return; } if (waiting[id]) { // Race condition: asyncFn(1, cb), asyncFn.clear(), asyncFn(1, cb) if (typeof waiting[id] === "function") waiting[id] = [waiting[id], currentCallback.cb]; else waiting[id].push(currentCallback.cb); } else { waiting[id] = currentCallback.cb; } delete currentCallback.cb; currentCallback.id = id; currentCallback = null; }); // On delete conf.on("delete", function (id) { var result; // If false, we don't have value yet, so we assume that intention is not // to memoize this call. After value is obtained we don't cache it but // gracefully pass to callback if (hasOwnProperty.call(waiting, id)) return; if (!cache[id]) return; result = cache[id]; delete cache[id]; conf.emit("deleteasync", id, slice.call(result.args, 1)); }); // On clear conf.on("clear", function () { var oldCache = cache; cache = create(null); conf.emit( "clearasync", objectMap(oldCache, function (data) { return slice.call(data.args, 1); }) ); }); }; memoizee-0.4.15/ext/dispose.js000066400000000000000000000015301377604527300162340ustar00rootroot00000000000000// Call dispose callback on each cache purge "use strict"; var callable = require("es5-ext/object/valid-callable") , forEach = require("es5-ext/object/for-each") , extensions = require("../lib/registered-extensions") , apply = Function.prototype.apply; extensions.dispose = function (dispose, conf, options) { var del; callable(dispose); if ((options.async && extensions.async) || (options.promise && extensions.promise)) { conf.on("deleteasync", del = function (id, resultArray) { apply.call(dispose, null, resultArray); }); conf.on("clearasync", function (cache) { forEach(cache, function (result, id) { del(id, result); }); }); return; } conf.on("delete", del = function (id, result) { dispose(result); }); conf.on("clear", function (cache) { forEach(cache, function (result, id) { del(id, result); }); }); }; memoizee-0.4.15/ext/max-age.js000066400000000000000000000053041377604527300161100ustar00rootroot00000000000000/* eslint consistent-this: 0 */ // Timeout cached values "use strict"; var aFrom = require("es5-ext/array/from") , forEach = require("es5-ext/object/for-each") , nextTick = require("next-tick") , isPromise = require("is-promise") , timeout = require("timers-ext/valid-timeout") , extensions = require("../lib/registered-extensions"); var noop = Function.prototype, max = Math.max, min = Math.min, create = Object.create; extensions.maxAge = function (maxAge, conf, options) { var timeouts, postfix, preFetchAge, preFetchTimeouts; maxAge = timeout(maxAge); if (!maxAge) return; timeouts = create(null); postfix = (options.async && extensions.async) || (options.promise && extensions.promise) ? "async" : ""; conf.on("set" + postfix, function (id) { timeouts[id] = setTimeout(function () { conf.delete(id); }, maxAge); if (typeof timeouts[id].unref === "function") timeouts[id].unref(); if (!preFetchTimeouts) return; if (preFetchTimeouts[id]) { if (preFetchTimeouts[id] !== "nextTick") clearTimeout(preFetchTimeouts[id]); } preFetchTimeouts[id] = setTimeout(function () { delete preFetchTimeouts[id]; }, preFetchAge); if (typeof preFetchTimeouts[id].unref === "function") preFetchTimeouts[id].unref(); }); conf.on("delete" + postfix, function (id) { clearTimeout(timeouts[id]); delete timeouts[id]; if (!preFetchTimeouts) return; if (preFetchTimeouts[id] !== "nextTick") clearTimeout(preFetchTimeouts[id]); delete preFetchTimeouts[id]; }); if (options.preFetch) { if (options.preFetch === true || isNaN(options.preFetch)) { preFetchAge = 0.333; } else { preFetchAge = max(min(Number(options.preFetch), 1), 0); } if (preFetchAge) { preFetchTimeouts = {}; preFetchAge = (1 - preFetchAge) * maxAge; conf.on("get" + postfix, function (id, args, context) { if (!preFetchTimeouts[id]) { preFetchTimeouts[id] = "nextTick"; nextTick(function () { var result; if (preFetchTimeouts[id] !== "nextTick") return; delete preFetchTimeouts[id]; conf.delete(id); if (options.async) { args = aFrom(args); args.push(noop); } result = conf.memoized.apply(context, args); if (options.promise) { // Supress eventual error warnings if (isPromise(result)) { if (typeof result.done === "function") result.done(noop, noop); else result.then(noop, noop); } } }); } }); } } conf.on("clear" + postfix, function () { forEach(timeouts, function (id) { clearTimeout(id); }); timeouts = {}; if (preFetchTimeouts) { forEach(preFetchTimeouts, function (id) { if (id !== "nextTick") clearTimeout(id); }); preFetchTimeouts = {}; } }); }; memoizee-0.4.15/ext/max.js000066400000000000000000000013411377604527300153530ustar00rootroot00000000000000// Limit cache size, LRU (least recently used) algorithm. "use strict"; var toPosInteger = require("es5-ext/number/to-pos-integer") , lruQueue = require("lru-queue") , extensions = require("../lib/registered-extensions"); extensions.max = function (max, conf, options) { var postfix, queue, hit; max = toPosInteger(max); if (!max) return; queue = lruQueue(max); postfix = (options.async && extensions.async) || (options.promise && extensions.promise) ? "async" : ""; conf.on("set" + postfix, hit = function (id) { id = queue.hit(id); if (id === undefined) return; conf.delete(id); }); conf.on("get" + postfix, hit); conf.on("delete" + postfix, queue.delete); conf.on("clear" + postfix, queue.clear); }; memoizee-0.4.15/ext/promise.js000066400000000000000000000104251377604527300162470ustar00rootroot00000000000000/* eslint max-statements: 0 */ // Support for functions returning promise "use strict"; var objectMap = require("es5-ext/object/map") , primitiveSet = require("es5-ext/object/primitive-set") , ensureString = require("es5-ext/object/validate-stringifiable-value") , toShortString = require("es5-ext/to-short-string-representation") , isPromise = require("is-promise") , nextTick = require("next-tick"); var create = Object.create , supportedModes = primitiveSet("then", "then:finally", "done", "done:finally"); require("../lib/registered-extensions").promise = function (mode, conf) { var waiting = create(null), cache = create(null), promises = create(null); if (mode === true) { mode = null; } else { mode = ensureString(mode); if (!supportedModes[mode]) { throw new TypeError("'" + toShortString(mode) + "' is not valid promise mode"); } } // After not from cache call conf.on("set", function (id, ignore, promise) { var isFailed = false; if (!isPromise(promise)) { // Non promise result cache[id] = promise; conf.emit("setasync", id, 1); return; } waiting[id] = 1; promises[id] = promise; var onSuccess = function (result) { var count = waiting[id]; if (isFailed) { throw new Error( "Memoizee error: Detected unordered then|done & finally resolution, which " + "in turn makes proper detection of success/failure impossible (when in " + "'done:finally' mode)\n" + "Consider to rely on 'then' or 'done' mode instead." ); } if (!count) return; // Deleted from cache before resolved delete waiting[id]; cache[id] = result; conf.emit("setasync", id, count); }; var onFailure = function () { isFailed = true; if (!waiting[id]) return; // Deleted from cache (or succeed in case of finally) delete waiting[id]; delete promises[id]; conf.delete(id); }; var resolvedMode = mode; if (!resolvedMode) resolvedMode = "then"; if (resolvedMode === "then") { var nextTickFailure = function () { nextTick(onFailure); }; // Eventual finally needs to be attached to non rejected promise // (so we not force propagation of unhandled rejection) promise = promise.then(function (result) { nextTick(onSuccess.bind(this, result)); }, nextTickFailure); // If `finally` is a function we attach to it to remove cancelled promises. if (typeof promise.finally === "function") { promise.finally(nextTickFailure); } } else if (resolvedMode === "done") { // Not recommended, as it may mute any eventual "Unhandled error" events if (typeof promise.done !== "function") { throw new Error( "Memoizee error: Retrieved promise does not implement 'done' " + "in 'done' mode" ); } promise.done(onSuccess, onFailure); } else if (resolvedMode === "done:finally") { // The only mode with no side effects assuming library does not throw unconditionally // for rejected promises. if (typeof promise.done !== "function") { throw new Error( "Memoizee error: Retrieved promise does not implement 'done' " + "in 'done:finally' mode" ); } if (typeof promise.finally !== "function") { throw new Error( "Memoizee error: Retrieved promise does not implement 'finally' " + "in 'done:finally' mode" ); } promise.done(onSuccess); promise.finally(onFailure); } }); // From cache (sync) conf.on("get", function (id, args, context) { var promise; if (waiting[id]) { ++waiting[id]; // Still waiting return; } promise = promises[id]; var emit = function () { conf.emit("getasync", id, args, context); }; if (isPromise(promise)) { if (typeof promise.done === "function") promise.done(emit); else { promise.then(function () { nextTick(emit); }); } } else { emit(); } }); // On delete conf.on("delete", function (id) { delete promises[id]; if (waiting[id]) { delete waiting[id]; return; // Not yet resolved } if (!hasOwnProperty.call(cache, id)) return; var result = cache[id]; delete cache[id]; conf.emit("deleteasync", id, [result]); }); // On clear conf.on("clear", function () { var oldCache = cache; cache = create(null); waiting = create(null); promises = create(null); conf.emit("clearasync", objectMap(oldCache, function (data) { return [data]; })); }); }; memoizee-0.4.15/ext/ref-counter.js000066400000000000000000000021601377604527300170170ustar00rootroot00000000000000// Reference counter, useful for garbage collector like functionality "use strict"; var d = require("d") , extensions = require("../lib/registered-extensions") , create = Object.create, defineProperties = Object.defineProperties; extensions.refCounter = function (ignore, conf, options) { var cache, postfix; cache = create(null); postfix = (options.async && extensions.async) || (options.promise && extensions.promise) ? "async" : ""; conf.on("set" + postfix, function (id, length) { cache[id] = length || 1; }); conf.on("get" + postfix, function (id) { ++cache[id]; }); conf.on("delete" + postfix, function (id) { delete cache[id]; }); conf.on("clear" + postfix, function () { cache = {}; }); defineProperties(conf.memoized, { deleteRef: d(function () { var id = conf.get(arguments); if (id === null) return null; if (!cache[id]) return null; if (!--cache[id]) { conf.delete(id); return true; } return false; }), getRefCount: d(function () { var id = conf.get(arguments); if (id === null) return 0; if (!cache[id]) return 0; return cache[id]; }) }); }; memoizee-0.4.15/index.js000066400000000000000000000023101377604527300150720ustar00rootroot00000000000000"use strict"; var normalizeOpts = require("es5-ext/object/normalize-options") , resolveLength = require("./lib/resolve-length") , plain = require("./plain"); module.exports = function (fn/*, options*/) { var options = normalizeOpts(arguments[1]), length; if (!options.normalizer) { length = options.length = resolveLength(options.length, fn.length, options.async); if (length !== 0) { if (options.primitive) { if (length === false) { options.normalizer = require("./normalizers/primitive"); } else if (length > 1) { options.normalizer = require("./normalizers/get-primitive-fixed")(length); } } else if (length === false) options.normalizer = require("./normalizers/get")(); else if (length === 1) options.normalizer = require("./normalizers/get-1")(); else options.normalizer = require("./normalizers/get-fixed")(length); } } // Assure extensions if (options.async) require("./ext/async"); if (options.promise) require("./ext/promise"); if (options.dispose) require("./ext/dispose"); if (options.maxAge) require("./ext/max-age"); if (options.max) require("./ext/max"); if (options.refCounter) require("./ext/ref-counter"); return plain(fn, options); }; memoizee-0.4.15/lib/000077500000000000000000000000001377604527300141775ustar00rootroot00000000000000memoizee-0.4.15/lib/configure-map.js000066400000000000000000000121301377604527300172660ustar00rootroot00000000000000/* eslint no-eq-null: 0, eqeqeq: 0, no-unused-vars: 0 */ "use strict"; var customError = require("es5-ext/error/custom") , defineLength = require("es5-ext/function/_define-length") , d = require("d") , ee = require("event-emitter").methods , resolveResolve = require("./resolve-resolve") , resolveNormalize = require("./resolve-normalize"); var apply = Function.prototype.apply , call = Function.prototype.call , create = Object.create , defineProperties = Object.defineProperties , on = ee.on , emit = ee.emit; module.exports = function (original, length, options) { var cache = create(null) , conf , memLength , get , set , del , clear , extDel , extGet , extHas , normalizer , getListeners , setListeners , deleteListeners , memoized , resolve; if (length !== false) memLength = length; else if (isNaN(original.length)) memLength = 1; else memLength = original.length; if (options.normalizer) { normalizer = resolveNormalize(options.normalizer); get = normalizer.get; set = normalizer.set; del = normalizer.delete; clear = normalizer.clear; } if (options.resolvers != null) resolve = resolveResolve(options.resolvers); if (get) { memoized = defineLength(function (arg) { var id, result, args = arguments; if (resolve) args = resolve(args); id = get(args); if (id !== null) { if (hasOwnProperty.call(cache, id)) { if (getListeners) conf.emit("get", id, args, this); return cache[id]; } } if (args.length === 1) result = call.call(original, this, args[0]); else result = apply.call(original, this, args); if (id === null) { id = get(args); if (id !== null) throw customError("Circular invocation", "CIRCULAR_INVOCATION"); id = set(args); } else if (hasOwnProperty.call(cache, id)) { throw customError("Circular invocation", "CIRCULAR_INVOCATION"); } cache[id] = result; if (setListeners) conf.emit("set", id, null, result); return result; }, memLength); } else if (length === 0) { memoized = function () { var result; if (hasOwnProperty.call(cache, "data")) { if (getListeners) conf.emit("get", "data", arguments, this); return cache.data; } if (arguments.length) result = apply.call(original, this, arguments); else result = call.call(original, this); if (hasOwnProperty.call(cache, "data")) { throw customError("Circular invocation", "CIRCULAR_INVOCATION"); } cache.data = result; if (setListeners) conf.emit("set", "data", null, result); return result; }; } else { memoized = function (arg) { var result, args = arguments, id; if (resolve) args = resolve(arguments); id = String(args[0]); if (hasOwnProperty.call(cache, id)) { if (getListeners) conf.emit("get", id, args, this); return cache[id]; } if (args.length === 1) result = call.call(original, this, args[0]); else result = apply.call(original, this, args); if (hasOwnProperty.call(cache, id)) { throw customError("Circular invocation", "CIRCULAR_INVOCATION"); } cache[id] = result; if (setListeners) conf.emit("set", id, null, result); return result; }; } conf = { original: original, memoized: memoized, profileName: options.profileName, get: function (args) { if (resolve) args = resolve(args); if (get) return get(args); return String(args[0]); }, has: function (id) { return hasOwnProperty.call(cache, id); }, delete: function (id) { var result; if (!hasOwnProperty.call(cache, id)) return; if (del) del(id); result = cache[id]; delete cache[id]; if (deleteListeners) conf.emit("delete", id, result); }, clear: function () { var oldCache = cache; if (clear) clear(); cache = create(null); conf.emit("clear", oldCache); }, on: function (type, listener) { if (type === "get") getListeners = true; else if (type === "set") setListeners = true; else if (type === "delete") deleteListeners = true; return on.call(this, type, listener); }, emit: emit, updateEnv: function () { original = conf.original; } }; if (get) { extDel = defineLength(function (arg) { var id, args = arguments; if (resolve) args = resolve(args); id = get(args); if (id === null) return; conf.delete(id); }, memLength); } else if (length === 0) { extDel = function () { return conf.delete("data"); }; } else { extDel = function (arg) { if (resolve) arg = resolve(arguments)[0]; return conf.delete(arg); }; } extGet = defineLength(function () { var id, args = arguments; if (length === 0) return cache.data; if (resolve) args = resolve(args); if (get) id = get(args); else id = String(args[0]); return cache[id]; }); extHas = defineLength(function () { var id, args = arguments; if (length === 0) return conf.has("data"); if (resolve) args = resolve(args); if (get) id = get(args); else id = String(args[0]); if (id === null) return false; return conf.has(id); }); defineProperties(memoized, { __memoized__: d(true), delete: d(extDel), clear: d(conf.clear), _get: d(extGet), _has: d(extHas) }); return conf; }; memoizee-0.4.15/lib/methods.js000066400000000000000000000016451377604527300162060ustar00rootroot00000000000000"use strict"; var forEach = require("es5-ext/object/for-each") , normalizeOpts = require("es5-ext/object/normalize-options") , callable = require("es5-ext/object/valid-callable") , lazy = require("d/lazy") , resolveLength = require("./resolve-length") , extensions = require("./registered-extensions"); module.exports = function (memoize) { return function (props) { forEach(props, function (desc) { var fn = callable(desc.value), length; desc.value = function (options) { if (options.getNormalizer) { options = normalizeOpts(options); if (length === undefined) { length = resolveLength( options.length, fn.length, options.async && extensions.async ); } options.normalizer = options.getNormalizer(length); delete options.getNormalizer; } return memoize(fn.bind(this), options); }; }); return lazy(props); }; }; memoizee-0.4.15/lib/registered-extensions.js000066400000000000000000000000161377604527300210640ustar00rootroot00000000000000"use strict"; memoizee-0.4.15/lib/resolve-length.js000066400000000000000000000005531377604527300174760ustar00rootroot00000000000000"use strict"; var toPosInt = require("es5-ext/number/to-pos-integer"); module.exports = function (optsLength, fnLength, isAsync) { var length; if (isNaN(optsLength)) { length = fnLength; if (!(length >= 0)) return 1; if (isAsync && length) return length - 1; return length; } if (optsLength === false) return false; return toPosInt(optsLength); }; memoizee-0.4.15/lib/resolve-normalize.js000066400000000000000000000011501377604527300202070ustar00rootroot00000000000000"use strict"; var callable = require("es5-ext/object/valid-callable"); module.exports = function (userNormalizer) { var normalizer; if (typeof userNormalizer === "function") return { set: userNormalizer, get: userNormalizer }; normalizer = { get: callable(userNormalizer.get) }; if (userNormalizer.set !== undefined) { normalizer.set = callable(userNormalizer.set); if (userNormalizer.delete) normalizer.delete = callable(userNormalizer.delete); if (userNormalizer.clear) normalizer.clear = callable(userNormalizer.clear); return normalizer; } normalizer.set = normalizer.get; return normalizer; }; memoizee-0.4.15/lib/resolve-resolve.js000066400000000000000000000011151377604527300176670ustar00rootroot00000000000000"use strict"; var toArray = require("es5-ext/array/to-array") , isValue = require("es5-ext/object/is-value") , callable = require("es5-ext/object/valid-callable"); var slice = Array.prototype.slice, resolveArgs; resolveArgs = function (args) { return this.map(function (resolve, i) { return resolve ? resolve(args[i]) : args[i]; }).concat(slice.call(args, this.length)); }; module.exports = function (resolvers) { resolvers = toArray(resolvers); resolvers.forEach(function (resolve) { if (isValue(resolve)) callable(resolve); }); return resolveArgs.bind(resolvers); }; memoizee-0.4.15/lib/weak.js000066400000000000000000000075261377604527300154760ustar00rootroot00000000000000"use strict"; var customError = require("es5-ext/error/custom") , defineLength = require("es5-ext/function/_define-length") , partial = require("es5-ext/function/#/partial") , copy = require("es5-ext/object/copy") , normalizeOpts = require("es5-ext/object/normalize-options") , callable = require("es5-ext/object/valid-callable") , d = require("d") , WeakMap = require("es6-weak-map") , resolveLength = require("./resolve-length") , extensions = require("./registered-extensions") , resolveResolve = require("./resolve-resolve") , resolveNormalize = require("./resolve-normalize"); var slice = Array.prototype.slice, defineProperties = Object.defineProperties; module.exports = function (memoize) { return function (fn/*, options*/) { var map, length, options = normalizeOpts(arguments[1]), memoized, resolve, normalizer; callable(fn); // Do not memoize already memoized function if (hasOwnProperty.call(fn, "__memoized__") && !options.force) return fn; length = resolveLength(options.length, fn.length, options.async && extensions.async); options.length = length ? length - 1 : 0; map = new WeakMap(); if (options.resolvers) resolve = resolveResolve(options.resolvers); if (options.normalizer) normalizer = resolveNormalize(options.normalizer); if ( length === 1 && !normalizer && !options.async && !options.dispose && !options.maxAge && !options.max && !options.refCounter ) { return defineProperties( function (obj) { var result, args = arguments; if (resolve) args = resolve(args); obj = args[0]; if (map.has(obj)) return map.get(obj); result = fn.apply(this, args); if (map.has(obj)) { throw customError("Circular invocation", "CIRCULAR_INVOCATION"); } map.set(obj, result); return result; }, { __memoized__: d(true), delete: d(function (obj) { if (resolve) obj = resolve(arguments)[0]; return map.delete(obj); }) } ); } memoized = defineProperties( defineLength(function (obj) { var memoizer, args = arguments; if (resolve) { args = resolve(args); obj = args[0]; } memoizer = map.get(obj); if (!memoizer) { if (normalizer) { options = copy(options); options.normalizer = copy(normalizer); options.normalizer.get = partial.call(options.normalizer.get, obj); options.normalizer.set = partial.call(options.normalizer.set, obj); if (options.normalizer.delete) { options.normalizer.delete = partial.call( options.normalizer.delete, obj ); } } map.set(obj, memoizer = memoize(partial.call(fn, obj), options)); } return memoizer.apply(this, slice.call(args, 1)); }, length), { __memoized__: d(true), delete: d( defineLength(function (obj) { var memoizer, args = arguments; if (resolve) { args = resolve(args); obj = args[0]; } memoizer = map.get(obj); if (!memoizer) return; memoizer.delete.apply(this, slice.call(args, 1)); }, length) ) } ); if (!options.refCounter) return memoized; defineProperties(memoized, { deleteRef: d( defineLength(function (obj) { var memoizer, args = arguments; if (resolve) { args = resolve(args); obj = args[0]; } memoizer = map.get(obj); if (!memoizer) return null; return memoizer.deleteRef.apply(this, slice.call(args, 1)); }, length) ), getRefCount: d( defineLength(function (obj) { var memoizer, args = arguments; if (resolve) { args = resolve(args); obj = args[0]; } memoizer = map.get(obj); if (!memoizer) return 0; return memoizer.getRefCount.apply(this, slice.call(args, 1)); }, length) ) }); return memoized; }; }; memoizee-0.4.15/methods-plain.js000066400000000000000000000001161377604527300165310ustar00rootroot00000000000000"use strict"; module.exports = require("./lib/methods")(require("./plain")); memoizee-0.4.15/methods.js000066400000000000000000000001111377604527300154230ustar00rootroot00000000000000"use strict"; module.exports = require("./lib/methods")(require("./")); memoizee-0.4.15/normalizers/000077500000000000000000000000001377604527300157765ustar00rootroot00000000000000memoizee-0.4.15/normalizers/get-1.js000066400000000000000000000011261377604527300172510ustar00rootroot00000000000000"use strict"; var indexOf = require("es5-ext/array/#/e-index-of"); module.exports = function () { var lastId = 0, argsMap = [], cache = []; return { get: function (args) { var index = indexOf.call(argsMap, args[0]); return index === -1 ? null : cache[index]; }, set: function (args) { argsMap.push(args[0]); cache.push(++lastId); return lastId; }, delete: function (id) { var index = indexOf.call(cache, id); if (index !== -1) { argsMap.splice(index, 1); cache.splice(index, 1); } }, clear: function () { argsMap = []; cache = []; } }; }; memoizee-0.4.15/normalizers/get-fixed.js000066400000000000000000000031001377604527300202020ustar00rootroot00000000000000"use strict"; var indexOf = require("es5-ext/array/#/e-index-of") , create = Object.create; module.exports = function (length) { var lastId = 0, map = [[], []], cache = create(null); return { get: function (args) { var index = 0, set = map, i; while (index < length - 1) { i = indexOf.call(set[0], args[index]); if (i === -1) return null; set = set[1][i]; ++index; } i = indexOf.call(set[0], args[index]); if (i === -1) return null; return set[1][i] || null; }, set: function (args) { var index = 0, set = map, i; while (index < length - 1) { i = indexOf.call(set[0], args[index]); if (i === -1) { i = set[0].push(args[index]) - 1; set[1].push([[], []]); } set = set[1][i]; ++index; } i = indexOf.call(set[0], args[index]); if (i === -1) { i = set[0].push(args[index]) - 1; } set[1][i] = ++lastId; cache[lastId] = args; return lastId; }, delete: function (id) { var index = 0, set = map, i, path = [], args = cache[id]; while (index < length - 1) { i = indexOf.call(set[0], args[index]); if (i === -1) { return; } path.push(set, i); set = set[1][i]; ++index; } i = indexOf.call(set[0], args[index]); if (i === -1) { return; } id = set[1][i]; set[0].splice(i, 1); set[1].splice(i, 1); while (!set[0].length && path.length) { i = path.pop(); set = path.pop(); set[0].splice(i, 1); set[1].splice(i, 1); } delete cache[id]; }, clear: function () { map = [[], []]; cache = create(null); } }; }; memoizee-0.4.15/normalizers/get-primitive-fixed.js000066400000000000000000000004311377604527300222140ustar00rootroot00000000000000"use strict"; module.exports = function (length) { if (!length) { return function () { return ""; }; } return function (args) { var id = String(args[0]), i = 0, currentLength = length; while (--currentLength) { id += "\u0001" + args[++i]; } return id; }; }; memoizee-0.4.15/normalizers/get.js000066400000000000000000000040211377604527300171100ustar00rootroot00000000000000/* eslint max-statements: 0 */ "use strict"; var indexOf = require("es5-ext/array/#/e-index-of"); var create = Object.create; module.exports = function () { var lastId = 0, map = [], cache = create(null); return { get: function (args) { var index = 0, set = map, i, length = args.length; if (length === 0) return set[length] || null; if ((set = set[length])) { while (index < length - 1) { i = indexOf.call(set[0], args[index]); if (i === -1) return null; set = set[1][i]; ++index; } i = indexOf.call(set[0], args[index]); if (i === -1) return null; return set[1][i] || null; } return null; }, set: function (args) { var index = 0, set = map, i, length = args.length; if (length === 0) { set[length] = ++lastId; } else { if (!set[length]) { set[length] = [[], []]; } set = set[length]; while (index < length - 1) { i = indexOf.call(set[0], args[index]); if (i === -1) { i = set[0].push(args[index]) - 1; set[1].push([[], []]); } set = set[1][i]; ++index; } i = indexOf.call(set[0], args[index]); if (i === -1) { i = set[0].push(args[index]) - 1; } set[1][i] = ++lastId; } cache[lastId] = args; return lastId; }, delete: function (id) { var index = 0, set = map, i, args = cache[id], length = args.length, path = []; if (length === 0) { delete set[length]; } else if ((set = set[length])) { while (index < length - 1) { i = indexOf.call(set[0], args[index]); if (i === -1) { return; } path.push(set, i); set = set[1][i]; ++index; } i = indexOf.call(set[0], args[index]); if (i === -1) { return; } id = set[1][i]; set[0].splice(i, 1); set[1].splice(i, 1); while (!set[0].length && path.length) { i = path.pop(); set = path.pop(); set[0].splice(i, 1); set[1].splice(i, 1); } } delete cache[id]; }, clear: function () { map = []; cache = create(null); } }; }; memoizee-0.4.15/normalizers/primitive.js000066400000000000000000000003131377604527300203410ustar00rootroot00000000000000"use strict"; module.exports = function (args) { var id, i, length = args.length; if (!length) return "\u0002"; id = String(args[i = 0]); while (--length) id += "\u0001" + args[++i]; return id; }; memoizee-0.4.15/package.json000066400000000000000000000023261377604527300157220ustar00rootroot00000000000000{ "name": "memoizee", "version": "0.4.15", "description": "Memoize/cache function results", "author": "Mariusz Nowak (http://www.medikoo.com/)", "keywords": [ "memoize", "memoizer", "cache", "memoization", "memo", "memcached", "hashing.", "storage", "caching", "memory", "gc", "weak", "garbage", "collector", "async" ], "repository": { "type": "git", "url": "git://github.com/medikoo/memoizee.git" }, "dependencies": { "d": "^1.0.1", "es5-ext": "^0.10.53", "es6-weak-map": "^2.0.3", "event-emitter": "^0.3.5", "is-promise": "^2.2.2", "lru-queue": "^0.1.0", "next-tick": "^1.1.0", "timers-ext": "^0.1.7" }, "devDependencies": { "bluebird": "^3.7.2", "eslint": "^5.16.0", "eslint-config-medikoo-es5": "^1.7.3", "plain-promise": "^0.1.1", "tad": "^2.0.1" }, "eslintConfig": { "extends": "medikoo-es5", "root": true, "globals": { "setTimeout": true, "clearTimeout": true }, "rules": { "max-lines-per-function": "off" } }, "scripts": { "lint": "eslint --ignore-path=.gitignore .", "test": "tad" }, "license": "ISC" } memoizee-0.4.15/plain.js000066400000000000000000000020621377604527300150720ustar00rootroot00000000000000"use strict"; var callable = require("es5-ext/object/valid-callable") , forEach = require("es5-ext/object/for-each") , extensions = require("./lib/registered-extensions") , configure = require("./lib/configure-map") , resolveLength = require("./lib/resolve-length"); module.exports = function self(fn /*, options */) { var options, length, conf; callable(fn); options = Object(arguments[1]); if (options.async && options.promise) { throw new Error("Options 'async' and 'promise' cannot be used together"); } // Do not memoize already memoized function if (hasOwnProperty.call(fn, "__memoized__") && !options.force) return fn; // Resolve length; length = resolveLength(options.length, fn.length, options.async && extensions.async); // Configure cache map conf = configure(fn, length, options); // Bind eventual extensions forEach(extensions, function (extFn, name) { if (options[name]) extFn(options[name], conf, options); }); if (self.__profiler__) self.__profiler__(conf); conf.updateEnv(); return conf.memoized; }; memoizee-0.4.15/profile.js000066400000000000000000000047771377604527300154460ustar00rootroot00000000000000// Gathers statistical data, and provides them in convinient form "use strict"; var partial = require("es5-ext/function/#/partial") , forEach = require("es5-ext/object/for-each") , pad = require("es5-ext/string/#/pad") , compact = require("es5-ext/array/#/compact") , d = require("d") , memoize = require("./plain"); var max = Math.max, stats = exports.statistics = {}; Object.defineProperty( memoize, "__profiler__", d(function (conf) { var id, source, data, stack; stack = new Error().stack; if ( !stack || !stack.split("\n").slice(3).some(function (line) { if (line.indexOf("/memoizee/") === -1 && line.indexOf(" (native)") === -1) { source = line.replace(/\n/g, "\\n").trim(); return true; } return false; }) ) { source = "unknown"; } id = compact.call([conf.profileName, source]).join(", "); if (!stats[id]) stats[id] = { initial: 0, cached: 0 }; data = stats[id]; conf.on("set", function () { ++data.initial; }); conf.on("get", function () { ++data.cached; }); }) ); exports.log = function () { var initial, cached, ordered, ipad, cpad, ppad, toPrc, log; initial = cached = 0; ordered = []; toPrc = function (initialCount, cachedCount) { if (!initialCount && !cachedCount) { return "0.00"; } return ((cachedCount / (initialCount + cachedCount)) * 100).toFixed(2); }; log = "------------------------------------------------------------\n"; log += "Memoize statistics:\n\n"; forEach( stats, function (data, name) { initial += data.initial; cached += data.cached; ordered.push([name, data]); }, null, function (nameA, nameB) { return ( this[nameB].initial + this[nameB].cached - (this[nameA].initial + this[nameA].cached) ); } ); ipad = partial.call(pad, " ", max(String(initial).length, "Init".length)); cpad = partial.call(pad, " ", max(String(cached).length, "Cache".length)); ppad = partial.call(pad, " ", "%Cache".length); log += ipad.call("Init") + " " + cpad.call("Cache") + " " + ppad.call("%Cache") + " Source location\n"; log += ipad.call(initial) + " " + cpad.call(cached) + " " + ppad.call(toPrc(initial, cached)) + " (all)\n"; ordered.forEach(function (data) { var name = data[0]; data = data[1]; log += ipad.call(data.initial) + " " + cpad.call(data.cached) + " " + ppad.call(toPrc(data.initial, data.cached)) + " " + name + "\n"; }); log += "------------------------------------------------------------\n"; return log; }; memoizee-0.4.15/test/000077500000000000000000000000001377604527300144105ustar00rootroot00000000000000memoizee-0.4.15/test/ext/000077500000000000000000000000001377604527300152105ustar00rootroot00000000000000memoizee-0.4.15/test/ext/async.js000066400000000000000000000176121377604527300166720ustar00rootroot00000000000000/* eslint id-length: 0, func-names: 0, handle-callback-err: 0, max-lines: 0 */ "use strict"; var memoize = require("../..") , nextTick = require("next-tick"); module.exports = function () { return { "Regular": { Success: function (a, d) { var mfn, fn, u = {}, i = 0, invoked = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true }); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Init Called"); a(invoked, 5, "Cb Called"); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Init Called #2"); a(invoked, 7, "Cb Called #2"); mfn.delete(3, 7); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Init After delete"); a(invoked, 9, "Cb After delete"); d(); }); }); }); }, Error: function (a, d) { var mfn, fn, u = {}, i = 0, e = new Error("Test"); fn = function (x, y, cb) { nextTick(function () { ++i; cb(e); }); return u; }; mfn = memoize(fn, { async: true, dispose: a.never }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Again Called #2"); d(); }); }); } }, "Primitive": { "Success": function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, primitive: true }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Again Called #2"); mfn.delete(3, 7); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Call After delete"); d(); }); }); }); }, "Error": function (a, d) { var mfn, fn, u = {}, i = 0, e = new Error("Test"); fn = function (x, y, cb) { nextTick(function () { ++i; cb(e); }); return u; }; mfn = memoize(fn, { async: true, primitive: true }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Again Called #2"); d(); }); }); }, "Primitive null arg case": function (a, d) { var x = {} , mfn = memoize( function f(id, cb) { cb(null, x); }, { async: true, primitive: true } ); mfn(null, function (err, res) { a.deep([err, res], [null, x], "Args"); d(); }); } }, "Sync Clear": function (a, d) { var mfn, fn; fn = function (x, cb) { nextTick(function () { cb(null, x); }); }; mfn = memoize(fn, { async: true }); mfn(1, function (err, i) { a(i, 1, "First"); }); mfn.clear(); mfn(2, function (err, i) { a(i, 2, "Second"); d(); }); }, "Sync Clear: Primitive": function (a, d) { var mfn, fn; fn = function (x, cb) { nextTick(function () { cb(null, x); }); }; mfn = memoize(fn, { async: true, primitive: true }); mfn(2, function (err, i) { a(i, 2, "First"); }); mfn(1, function (err, i) { a(i, 1, "Second"); nextTick(d); }); mfn.clear(); mfn(2, function (err, i) { a(i, 2, "Third"); }); } }; }; memoizee-0.4.15/test/ext/dispose.js000066400000000000000000000130701377604527300172150ustar00rootroot00000000000000/* eslint id-length: 0, max-lines: 0, max-statements: 0 */ "use strict"; var memoize = require("../..") , nextTick = require("next-tick") , delay = require("timers-ext/delay") , Promise = require("plain-promise"); module.exports = function () { return { Regular: { "Sync": function (a) { var mfn, fn, value = [], x, invoked; fn = function (arg1, arg2) { return arg1 + arg2; }; mfn = memoize(fn, { dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); x = {}; invoked = false; mfn = memoize(function () { return x; }, { dispose: function (val) { invoked = val; } }); mfn.delete(); a(invoked, false, "No args: Post invalid delete"); mfn(); a(invoked, false, "No args: Post cache"); mfn.delete(); a(invoked, x, "No args: Pre delete"); }, "Ref counter": function (a) { var mfn, fn, value = []; fn = function (x, y) { return x + y; }; mfn = memoize(fn, { refCounter: true, dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn(5, 8); mfn.deleteRef(5, 8); a.deep(value, [], "Pre"); mfn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; mfn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); }, "Async": function (a, d) { var mfn, fn, u = {}, value = []; fn = function (x, y, cb) { nextTick(function () { cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, dispose: function (val) { value.push(val); } }); mfn(3, 7, function () { mfn(5, 8, function () { mfn(12, 4, function () { a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11, function () { mfn.clear(); a.deep(value, [10, 88], "Clear all"); d(); }); }); }); }); }, "Promise": function (a, d) { var mfn, fn, value = []; fn = function (x, y) { return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { promise: true, dispose: function (val) { value.push(val); } }); mfn(3, 7).done(function () { mfn(5, 8).done(function () { mfn(12, 4).done( delay(function () { a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11).done( delay(function () { mfn.clear(); a.deep(value, [10, 88], "Clear all"); d(); }) ); }) ); }); }); } }, Primitive: { "Sync": function (a) { var mfn, fn, value = []; fn = function (x, y) { return x + y; }; mfn = memoize(fn, { dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); }, "Ref counter": function (a) { var mfn, fn, value = []; fn = function (x, y) { return x + y; }; mfn = memoize(fn, { refCounter: true, dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn(5, 8); mfn.deleteRef(5, 8); a.deep(value, [], "Pre"); mfn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; mfn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); }, "Async": function (a, d) { var mfn, fn, u = {}, value = []; fn = function (x, y, cb) { nextTick(function () { cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, dispose: function (val) { value.push(val); } }); mfn(3, 7, function () { mfn(5, 8, function () { mfn(12, 4, function () { a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11, function () { mfn.clear(); a.deep(value, [10, 88], "Clear all"); d(); }); }); }); }); }, "Promise": function (a, d) { var mfn, fn, value = []; fn = function (x, y) { return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { promise: true, dispose: function (val) { value.push(val); } }); mfn(3, 7).done(function () { mfn(5, 8).done(function () { mfn(12, 4).done( delay(function () { a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11).done( delay(function () { mfn.clear(); a.deep(value, [10, 88], "Clear all"); d(); }) ); }) ); }); }); } } }; }; memoizee-0.4.15/test/ext/max-age.js000066400000000000000000000627161377604527300171010ustar00rootroot00000000000000/* eslint max-lines: 0, id-length: 0, func-names: 0, handle-callback-err: 0, max-lines: 0, no-unused-vars: 0, max-nested-callbacks: 0, no-shadow: 0, max-len: 0 */ "use strict"; var memoize = require("../..") , nextTick = require("next-tick") , delay = require("timers-ext/delay") , Promise = require("plain-promise"); require("../../ext/async"); require("../../ext/promise"); module.exports = function () { return { Regular: { Sync: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = memoize(fn, { maxAge: 100 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait"); a(i, 2, "Called: Wait"); a(mfn(5, 8), 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); a(mfn(9, 1), 10, "Result: C"); a(i, 3, "Called: C"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait After"); a(i, 4, "Called: Wait After"); a(mfn(5, 8), 13, "Result: Wait After B"); a(i, 5, "Called: Wait After B"); a(mfn(3, 7), 10, "Result: Wait After #2"); a(i, 5, "Called: Wait After #2"); a(mfn(5, 8), 13, "Result: Wait After B #2"); a(i, 5, "Called: Wait After B #2"); a(mfn(9, 1), 10, "Result: WiatC"); a(i, 5, "Called: Wait C"); d(); }, 70); }, 40); }, Promise: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { return new Promise(function (res) { ++i; res(x + y); }); }; mfn = memoize(fn, { promise: true, maxAge: 100 }); mfn(3, 7).done(function (res) { a(res, 10, "Result #1"); }); mfn(5, 8).done(function (res) { a(res, 13, "Result #2"); }); mfn(3, 7).done(function (res) { a(res, 10, "Result #3"); }); mfn(3, 7).done(function (res) { a(res, 10, "Result #4"); }); mfn(5, 8).done(function (res) { a(res, 13, "Result #5"); }); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result #1"); }); mfn(5, 8).done(function (res) { a(res, 13, "Again: Result #2"); }); setTimeout(function () { a(i, 2, "Again Called #2"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result #1"); }); mfn(5, 8).done(function (res) { a(res, 13, "Again: Result #2"); }); nextTick(function () { a(i, 4, "Call After clear"); d(); }); }, 100); }, 20); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, maxAge: 100 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); setTimeout(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); setTimeout(function () { a(i, 2, "Again Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Call After clear"); d(); }); }, 100); }, 20); } }, Primitive: { Sync: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = memoize(fn, { primitive: true, maxAge: 100 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait"); a(i, 2, "Called: Wait"); a(mfn(5, 8), 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait After"); a(i, 3, "Called: Wait After"); a(mfn(5, 8), 13, "Result: Wait After B"); a(i, 4, "Called: Wait After B"); a(mfn(3, 7), 10, "Result: Wait After #2"); a(i, 4, "Called: Wait After #2"); a(mfn(5, 8), 13, "Result: Wait After B #2"); a(i, 4, "Called: Wait After B #2"); d(); }, 100); }, 20); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, primitive: true, maxAge: 100 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); setTimeout(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); setTimeout(function () { a(i, 2, "Again Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Call After clear"); d(); }); }, 100); }, 20); }, Promise: function (a, d) { var mfn, fn, i = 0; fn = function (x, y, cb) { return new Promise(function (res) { ++i; res(x + y); }); }; mfn = memoize(fn, { promise: true, primitive: true, maxAge: 500 }); mfn(3, 7).done(function (res) { a.deep(res, 10, "Result #1"); }); mfn(3, 7).done(function (res) { a.deep(res, 10, "Result #2"); }); mfn(5, 8).done(function (res) { a.deep(res, 13, "Result B #1"); }); mfn(3, 7).done(function (res) { a.deep(res, 10, "Result #3"); }); mfn(5, 8).done(function (res) { a.deep(res, 13, "Result B #2"); }); // 20 setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(function (res) { a.deep(res, 10, "Again: Result"); }); mfn(5, 8).done(function (res) { a.deep(res, 13, "Again B: Result"); }); // 100 setTimeout(function () { a(i, 2, "Again Called #2"); mfn(3, 7).done(function (res) { a.deep(res, 10, "Again: Result"); }); mfn(5, 8).done(function (res) { a.deep(res, 13, "Again B: Result"); }); nextTick(function () { a(i, 4, "Call After clear"); d(); }); }, 500); }, 100); } }, Refetch: { Default: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = memoize(fn, { maxAge: 600, preFetch: true }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait"); a(i, 2, "Called: Wait"); a(mfn(5, 8), 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait After"); a(i, 2, "Called: Wait After"); a(mfn(5, 8), 13, "Result: Wait After B"); a(i, 2, "Called: Wait After B"); a(mfn(3, 7), 10, "Result: Wait After #2"); a(i, 2, "Called: Wait After #2"); a(mfn(5, 8), 13, "Result: Wait After B #2"); a(i, 2, "Called: Wait After B #2"); setTimeout(function () { a(i, 4, "Called: After Refetch: Before"); a(mfn(3, 7), 10, "Result: After Refetch"); a(i, 4, "Called: After Refetch: After"); a(mfn(5, 8), 13, "Result: After Refetch B"); a(i, 4, "Called: After Refetch B: After"); setTimeout(function () { a(mfn(3, 7), 10, "Result: After Refetch #2"); a(i, 4, "Called: After Refetch #2"); a(mfn(5, 8), 13, "Result: After Refetch #2 B"); a(i, 4, "Called: After Refetch #2 B"); a(mfn(3, 7), 10, "Result: After Refetch #3"); a(i, 4, "Called: After Refetch #3"); a(mfn(5, 8), 13, "Result: After Refetch #3 B"); a(i, 4, "Called: After Refetch #3 B"); d(); }, 200); }, 200); }, 200); }, 300); }, Async: function (a, d) { var mfn, fn, i = 0; fn = function (x, y, cb) { ++i; setTimeout(function () { cb(null, x + y); }, 0); }; mfn = memoize(fn, { maxAge: 600, preFetch: true, async: true }); // 1.Start mfn(3, 7, function (err, result) { a(result, 10, "Result #1"); a(i, 1, "Called #1"); mfn(3, 7, function (err, result) { a(result, 10, "Result #2"); a(i, 1, "Called #2"); mfn(5, 8, function (err, result) { a(result, 13, "Result B #1"); a(i, 2, "Called B #1"); mfn(3, 7, function (err, result) { a(result, 10, "Result #3"); a(i, 2, "Called #3"); mfn(5, 8, function (err, result) { a(result, 13, "Result B #2"); a(i, 2, "Called B #2"); // 2. Wait 300ms setTimeout(function () { // From cache, prefetch not triggered mfn(3, 7, function (err, result) { a(result, 10, "Result: Wait"); a(i, 2, "Called: Wait"); mfn(5, 8, function (err, result) { a(result, 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); // Wait 200ms setTimeout(function () { // From cache, prefetch triggered mfn(3, 7, function (err, result) { a(result, 10, "Result: Wait After"); a(i, 2, "Called: Wait After"); mfn(5, 8, function (err, result) { a(result, 13, "Result: Wait After B"); a(i, 3, "Called: Wait After B"); mfn(3, 7, function (err, result) { a( result, 10, "Result: Wait After #2" ); a(i, 4, "Called: Wait After #2"); mfn(5, 8, function (err, result) { a( result, 13, "Result: Wait After B #2" ); a( i, 4, "Called: Wait After B #2" ); // Wait 200ms setTimeout(function () { // From cache, prefetch not triggered a( i, 4, "Called: After Refetch: Before" ); mfn(3, 7, function ( err, result ) { a( result, 10, "Result: After Refetch" ); a( i, 4, "Called: After Refetch: After" ); mfn(5, 8, function ( err, result ) { a( result, 13, "Result: After Refetch B" ); a( i, 4, "Called: After Refetch B: After" ); // Wait 250ms setTimeout( function () { // From cache, prefetch triggered mfn( 3, 7, function ( err, result ) { a( result, 10, "Result: After Refetch #2" ); a( i, 4, "Called: After Refetch #2" ); mfn( 5, 8, function ( err, result ) { a( result, 13, "Result: After Refetch #2 B" ); a( i, 5, "Called: After Refetch #2 B" ); mfn( 3, 7, function ( err, result ) { a( result, 10, "Result: After Refetch #3" ); a( i, 6, "Called: After Refetch #3" ); mfn( 5, 8, function ( err, result ) { a( result, 13, "Result: After Refetch #3 B" ); a( i, 6, "Called: After Refetch #3 B" ); d(); } ); } ); } ); } ); }, 250 ); }); }); }, 200); }); }); }); }); }, 200); }); }); }, 300); }); }); }); }); }); }, Promise: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { maxAge: 1200, preFetch: true, promise: true }); mfn(3, 7).done(function (result) { a(result, 10, "Result #1"); a(i, 1, "Called #1"); mfn(3, 7).done(function (result) { a(result, 10, "Result #2"); a(i, 1, "Called #2"); mfn(5, 8).done(function (result) { a(result, 13, "Result B #1"); a(i, 2, "Called B #1"); mfn(3, 7).done(function (result) { a(result, 10, "Result #3"); a(i, 2, "Called #3"); mfn(5, 8).done(function (result) { a(result, 13, "Result B #2"); a(i, 2, "Called B #2"); // 600 setTimeout(function () { mfn(3, 7).done(function (result) { a(result, 10, "Result: Wait"); a(i, 2, "Called: Wait"); mfn(5, 8).done(function (result) { a(result, 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); // 400 setTimeout(function () { mfn(3, 7).done( // 0 delay(function (result) { a(result, 10, "Result: Wait After"); a(i, 3, "Called: Wait After"); mfn(5, 8).done( // 0 delay(function (result) { a( result, 13, "Result: Wait After B" ); a(i, 4, "Called: Wait After B"); mfn(3, 7).done( // next tick delay(function (result) { a( result, 10, "Result: Wait After #2" ); a( i, 4, "Called: Wait After #2" ); mfn(5, 8).done( function (result) { a( result, 13, "Result: Wait After B #2" ); a( i, 4, "Called: Wait After B #2" ); // 400 setTimeout( function () { a( i, 4, "Called: After Refetch: Before" ); mfn( 3, 7 ).done( function ( result ) { a( result, 10, "Result: After Refetch" ); a( i, 4, "Called: After Refetch: After" ); mfn( 5, 8 ).done( function ( result ) { a( result, 13, "Result: After Refetch B" ); a( i, 4, "Called: After Refetch B: After" ); // 400 setTimeout( function () { mfn( 3, 7 ).done( // 0 delay( function ( result ) { a( result, 10, "Result: After Refetch #2" ); a( i, 5, "Called: After Refetch #2" ); mfn( 5, 8 ).done( // 0 delay( function ( result ) { a( result, 13, "Result: After Refetch #2 B" ); a( i, 6, "Called: After Refetch #2 B" ); mfn( 3, 7 ).done( function ( result ) { a( result, 10, "Result: After Refetch #3" ); a( i, 6, "Called: After Refetch #3" ); mfn( 5, 8 ).done( function ( result ) { a( result, 13, "Result: After Refetch #3 B" ); a( i, 6, "Called: After Refetch #3 B" ); d(); } ); } ); }, 0 ) ); }, 0 ) ); }, 400 ); } ); } ); }, 400 ); } ); }) ); }, 0) ); }, 0) ); }, 400); }); }); }, 600); }); }); }); }); }); }, Custom: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = memoize(fn, { maxAge: 6000, preFetch: 1 / 6 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait"); a(i, 2, "Called: Wait"); a(mfn(5, 8), 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait After"); a(i, 2, "Called: Wait After"); a(mfn(5, 8), 13, "Result: Wait After B"); a(i, 2, "Called: Wait After B"); a(mfn(3, 7), 10, "Result: Wait After #2"); a(i, 2, "Called: Wait After #2"); a(mfn(5, 8), 13, "Result: Wait After B #2"); a(i, 2, "Called: Wait After B #2"); setTimeout(function () { a(i, 4, "Called: After Refetch: Before"); a(mfn(3, 7), 10, "Result: After Refetch"); a(i, 4, "Called: After Refetch: After"); a(mfn(5, 8), 13, "Result: After Refetch B"); a(i, 4, "Called: After Refetch B: After"); setTimeout(function () { a(mfn(3, 7), 10, "Result: After Refetch #2"); a(i, 4, "Called: After Refetch #2"); a(mfn(5, 8), 13, "Result: After Refetch #2 B"); a(i, 4, "Called: After Refetch #2 B"); a(mfn(3, 7), 10, "Result: After Refetch #3"); a(i, 4, "Called: After Refetch #3"); a(mfn(5, 8), 13, "Result: After Refetch #3 B"); a(i, 4, "Called: After Refetch #3 B"); d(); }, 2000); }, 3000); }, 1000); }, 4500); } } }; }; memoizee-0.4.15/test/ext/max.js000066400000000000000000001647631377604527300163540ustar00rootroot00000000000000/* eslint id-length: 0, max-lines: 0, max-statements: 0, max-nested-callbacks: 0, no-shadow: 0, max-len: 0 */ "use strict"; var memoize = require("../..") , nextTick = require("next-tick") , delay = require("timers-ext/delay") , Promise = require("plain-promise"); module.exports = function () { return { Regular: { Sync: function (a) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = memoize(fn, { max: 3 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); a(mfn(12, 4), 16, "Result C #1"); a(i, 3, "Called C #1"); a(mfn(3, 7), 10, "Result #4"); a(i, 3, "Called #4"); a(mfn(5, 8), 13, "Result B #3"); a(i, 3, "Called B #3"); a(mfn(77, 11), 88, "Result D #1"); // Delete 12, 4 a(i, 4, "Called D #1"); a(mfn(5, 8), 13, "Result B #4"); a(i, 4, "Called B #4"); a(mfn(12, 4), 16, "Result C #2"); // Delete 3, 7 a(i, 5, "Called C #2"); a(mfn(3, 7), 10, "Result #5"); // Delete 77, 11 a(i, 6, "Called #5"); a(mfn(77, 11), 88, "Result D #2"); // Delete 5, 8 a(i, 7, "Called D #2"); a(mfn(12, 4), 16, "Result C #3"); a(i, 7, "Called C #3"); a(mfn(5, 8), 13, "Result B #5"); // Delete 3, 7 a(i, 8, "Called B #5"); a(mfn(77, 11), 88, "Result D #3"); a(i, 8, "Called D #3"); mfn.delete(77, 11); a(mfn(77, 11), 88, "Result D #4"); a(i, 9, "Called D #4"); mfn.clear(); a(mfn(5, 8), 13, "Result B #6"); a(i, 10, "Called B #6"); a(mfn(77, 11), 88, "Result D #5"); a(i, 11, "Called D #5"); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, max: 3 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); a(i, 1, "Called #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); a(i, 1, "Called #2"); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); a(i, 2, "Called B #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); a(i, 2, "Called #3"); a( mfn(5, 8, function (err, res) { a.deep( [err, res], [null, 13], "Result B #2" ); a(i, 2, "Called B #2"); a( mfn(12, 4, function (err, res) { a.deep( [err, res], [null, 16], "Result C #1" ); a(i, 3, "Called C #1"); a( mfn(3, 7, function (err, res) { a.deep( [err, res], [null, 10], "Result #4" ); a(i, 3, "Called #4"); a( mfn(5, 8, function ( err, res ) { a.deep( [err, res], [null, 13], "Result B #3" ); a( i, 3, "Called B #3" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #1" ); a( i, 4, "Called D #1" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #4" ); a( i, 4, "Called B #4" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #2" ); a( i, 5, "Called C #2" ); a( mfn( 3, 7, function ( err, res ) { a.deep( [ err, res ], [ null, 10 ], "Result #5" ); a( i, 6, "Called #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #2" ); a( i, 7, "Called D #2" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #3" ); a( i, 7, "Called C #3" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #5" ); a( i, 8, "Called B #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #3" ); a( i, 8, "Called D #3" ); mfn.delete( 77, 11 ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #4" ); a( i, 9, "Called D #4" ); mfn.clear(); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #6" ); a( i, 10, "Called B #6" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #5" ); a( i, 11, "Called D #5" ); d(); } ), u, "Initial D #5" ); } ), u, "Initial B #6" ); } ), u, "Initial D #4" ); } ), u, "Initial D #3" ); } ), u, "Initial B #5" ); } ), u, "Initial C #3" ); } ), u, "Initial D #2" ); } ), u, "Initial #5" ); } ), u, "Initial C #2" ); } ), u, "Initial B #4" ); } ), u, "Initial D #1" ); }), u, "Initial B #3" ); }), u, "Initial #4" ); }), u, "Initial C #1" ); }), u, "Initial B #2" ); }), u, "Initial #3" ); }), u, "Initial B #1" ); }), u, "Initial #2" ); }), u, "Initial #1" ); }, Promise: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { promise: true, max: 3 }); mfn(3, 7).done( delay(function (res) { // [3,7] a(res, 10, "Result #1"); a(i, 1, "Called #1"); mfn(3, 7).done( delay(function (res) { a(res, 10, "Result #2"); a(i, 1, "Called #2"); mfn(5, 8).done( delay(function (res) { // [3,7], [5,8] a(res, 13, "Result B #1"); a(i, 2, "Called B #1"); mfn(3, 7).done( delay(function (res) { // [5,8], [3, 7] a(res, 10, "Result #3"); a(i, 2, "Called #3"); mfn(5, 8).done( delay(function (res) { // [3,7], [5,8] a(res, 13, "Result B #2"); a(i, 2, "Called B #2"); mfn(12, 4).done( delay(function (res) { // [3,7], [5,8], [12, 4] a(res, 16, "Result C #1"); a(i, 3, "Called C #1"); mfn(3, 7).done( delay(function (res) { // [5,8], [12, 4], [3, 7] a(res, 10, "Result #4"); a(i, 3, "Called #4"); mfn(5, 8).done( delay(function (res) { // [12, 4], [3, 7], [5, 8] a( res, 13, "Result B #3" ); a( i, 3, "Called B #3" ); mfn(77, 11).done( delay(function ( res ) { // [3, 7], [5, 8], [77, 11] a( res, 88, "Result D #1" ); a( i, 4, "Called D #1" ); mfn( 5, 8 ).done( delay( function ( res ) { // [3, 7], [77, 11], [5, 8] a( res, 13, "Result B #4" ); a( i, 4, "Called B #4" ); mfn( 12, 4 ).done( delay( function ( res ) { // [77, 11], [5, 8], [12, 4] a( res, 16, "Result C #2" ); a( i, 5, "Called C #2" ); mfn( 3, 7 ).done( delay( function ( res ) { a( res, 10, "Result #5" ); a( i, 6, "Called #5" ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #2" ); a( i, 7, "Called D #2" ); mfn( 12, 4 ).done( delay( function ( res ) { a( res, 16, "Result C #3" ); a( i, 7, "Called C #3" ); mfn( 5, 8 ).done( delay( function ( res ) { a( res, 13, "Result B #5" ); a( i, 8, "Called B #5" ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #3" ); a( i, 8, "Called D #3" ); mfn.delete( 77, 11 ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #4" ); a( i, 9, "Called D #4" ); mfn.clear(); mfn( 5, 8 ).done( delay( function ( res ) { a( res, 13, "Result B #6" ); a( i, 10, "Called B #6" ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #5" ); a( i, 11, "Called D #5" ); d(); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); }) ); }) ); }) ); }) ); }) ); }) ); }) ); }) ); }) ); } }, Primitive: { Sync: function (a) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = memoize(fn, { primitive: true, max: 3 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); a(mfn(12, 4), 16, "Result C #1"); a(i, 3, "Called C #1"); a(mfn(3, 7), 10, "Result #4"); a(i, 3, "Called #4"); a(mfn(5, 8), 13, "Result B #3"); a(i, 3, "Called B #3"); a(mfn(77, 11), 88, "Result D #1"); // Delete 12, 4 a(i, 4, "Called D #1"); a(mfn(5, 8), 13, "Result B #4"); a(i, 4, "Called B #4"); a(mfn(12, 4), 16, "Result C #2"); // Delete 3, 7 a(i, 5, "Called C #2"); a(mfn(3, 7), 10, "Result #5"); // Delete 77, 11 a(i, 6, "Called #5"); a(mfn(77, 11), 88, "Result D #2"); // Delete 5, 8 a(i, 7, "Called D #2"); a(mfn(12, 4), 16, "Result C #3"); a(i, 7, "Called C #3"); a(mfn(5, 8), 13, "Result B #5"); // Delete 3, 7 a(i, 8, "Called B #5"); a(mfn(77, 11), 88, "Result D #3"); a(i, 8, "Called D #3"); mfn.delete(77, 11); a(mfn(77, 11), 88, "Result D #4"); a(i, 9, "Called D #4"); mfn.clear(); a(mfn(5, 8), 13, "Result B #6"); a(i, 10, "Called B #6"); a(mfn(77, 11), 88, "Result D #5"); a(i, 11, "Called D #5"); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, primitive: true, max: 3 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); a(i, 1, "Called #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); a(i, 1, "Called #2"); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); a(i, 2, "Called B #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); a(i, 2, "Called #3"); a( mfn(5, 8, function (err, res) { a.deep( [err, res], [null, 13], "Result B #2" ); a(i, 2, "Called B #2"); a( mfn(12, 4, function (err, res) { a.deep( [err, res], [null, 16], "Result C #1" ); a(i, 3, "Called C #1"); a( mfn(3, 7, function (err, res) { a.deep( [err, res], [null, 10], "Result #4" ); a(i, 3, "Called #4"); a( mfn(5, 8, function ( err, res ) { a.deep( [err, res], [null, 13], "Result B #3" ); a( i, 3, "Called B #3" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #1" ); a( i, 4, "Called D #1" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #4" ); a( i, 4, "Called B #4" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #2" ); a( i, 5, "Called C #2" ); a( mfn( 3, 7, function ( err, res ) { a.deep( [ err, res ], [ null, 10 ], "Result #5" ); a( i, 6, "Called #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #2" ); a( i, 7, "Called D #2" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #3" ); a( i, 7, "Called C #3" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #5" ); a( i, 8, "Called B #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #3" ); a( i, 8, "Called D #3" ); mfn.delete( 77, 11 ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #4" ); a( i, 9, "Called D #4" ); mfn.clear(); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #6" ); a( i, 10, "Called B #6" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #5" ); a( i, 11, "Called D #5" ); d(); } ), u, "Initial D #5" ); } ), u, "Initial B #6" ); } ), u, "Initial D #4" ); } ), u, "Initial D #3" ); } ), u, "Initial B #5" ); } ), u, "Initial C #3" ); } ), u, "Initial D #2" ); } ), u, "Initial #5" ); } ), u, "Initial C #2" ); } ), u, "Initial B #4" ); } ), u, "Initial D #1" ); }), u, "Initial B #3" ); }), u, "Initial #4" ); }), u, "Initial C #1" ); }), u, "Initial B #2" ); }), u, "Initial #3" ); }), u, "Initial B #1" ); }), u, "Initial #2" ); }), u, "Initial #1" ); }, Promise: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { promise: true, primitive: true, max: 3 }); mfn(3, 7).done( delay(function (res) { a(res, 10, "Result #1"); a(i, 1, "Called #1"); mfn(3, 7).done( delay(function (res) { a(res, 10, "Result #2"); a(i, 1, "Called #2"); mfn(5, 8).done( delay(function (res) { a(res, 13, "Result B #1"); a(i, 2, "Called B #1"); mfn(3, 7).done( delay(function (res) { a(res, 10, "Result #3"); a(i, 2, "Called #3"); mfn(5, 8).done( delay(function (res) { a(res, 13, "Result B #2"); a(i, 2, "Called B #2"); mfn(12, 4).done( delay(function (res) { a(res, 16, "Result C #1"); a(i, 3, "Called C #1"); mfn(3, 7).done( delay(function (res) { a(res, 10, "Result #4"); a(i, 3, "Called #4"); mfn(5, 8).done( delay(function (res) { a( res, 13, "Result B #3" ); a( i, 3, "Called B #3" ); mfn(77, 11).done( delay(function ( res ) { a( res, 88, "Result D #1" ); a( i, 4, "Called D #1" ); mfn( 5, 8 ).done( delay( function ( res ) { a( res, 13, "Result B #4" ); a( i, 4, "Called B #4" ); mfn( 12, 4 ).done( delay( function ( res ) { a( res, 16, "Result C #2" ); a( i, 5, "Called C #2" ); mfn( 3, 7 ).done( delay( function ( res ) { a( res, 10, "Result #5" ); a( i, 6, "Called #5" ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #2" ); a( i, 7, "Called D #2" ); mfn( 12, 4 ).done( delay( function ( res ) { a( res, 16, "Result C #3" ); a( i, 7, "Called C #3" ); mfn( 5, 8 ).done( delay( function ( res ) { a( res, 13, "Result B #5" ); a( i, 8, "Called B #5" ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #3" ); a( i, 8, "Called D #3" ); mfn.delete( 77, 11 ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #4" ); a( i, 9, "Called D #4" ); mfn.clear(); mfn( 5, 8 ).done( delay( function ( res ) { a( res, 13, "Result B #6" ); a( i, 10, "Called B #6" ); mfn( 77, 11 ).done( delay( function ( res ) { a( res, 88, "Result D #5" ); a( i, 11, "Called D #5" ); d(); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); } ) ); }) ); }) ); }) ); }) ); }) ); }) ); }) ); }) ); }) ); } } }; }; memoizee-0.4.15/test/ext/promise.js000066400000000000000000000164271377604527300172360ustar00rootroot00000000000000/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0, max-lines: 0 */ "use strict"; var memoize = require("../..") , nextTick = require("next-tick") , Promise = require("plain-promise") , Bluebird = require("bluebird"); Bluebird.config({ cancellation: true }); module.exports = function () { return { "Regular": { Success: function (a, d) { var mfn, fn, i = 0, invoked = 0; fn = function (x, y) { return new Promise(function (res) { ++i; res(x + y); }); }; mfn = memoize(fn, { promise: true }); mfn(3, 7).done(function (res) { ++invoked; a(res, 10, "Result #1"); }, a.never); mfn(3, 7).done(function (res) { ++invoked; a(res, 10, "Result #2"); }, a.never); mfn(5, 8).done(function (res) { ++invoked; a(res, 13, "Result B #1"); }, a.never); mfn(3, 7).done(function (res) { ++invoked; a(res, 10, "Result #3"); }, a.never); mfn(5, 8).done(function (res) { ++invoked; a(res, 13, "Result B #2"); }, a.never); setTimeout(function () { a(i, 2, "Init Called"); a(invoked, 5, "Cb Called"); mfn(3, 7).done(function (res) { ++invoked; a(res, 10, "Again: Result"); }, a.never); mfn(5, 8).done(function (res) { ++invoked; a(res, 13, "Again B: Result"); }, a.never); setTimeout(function () { a(i, 2, "Init Called #2"); a(invoked, 7, "Cb Called #2"); mfn.delete(3, 7); mfn(3, 7).done(function (res) { ++invoked; a(res, 10, "Again: Result"); }, a.never); mfn(5, 8).done(function (res) { ++invoked; a(res, 13, "Again B: Result"); }, a.never); setTimeout(function () { a(i, 3, "Init After delete"); a(invoked, 9, "Cb After delete"); d(); }, 100); }, 100); }, 100); }, Error: function (a, d) { var mfn, fn, i = 0, e = new Error("Test"); fn = function (x, y) { ++i; return new Promise(function (res, rej) { rej(e); }); }; mfn = memoize(fn, { promise: "done", dispose: a.never }); mfn(3, 7).done(a.never, function (err) { a(err, e, "Result #1"); }); mfn(5, 8).done(a.never, function (err) { a(err, e, "Result B #2"); }); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(a.never, function (err) { a(err, e, "Again: Result"); }); mfn(5, 8).done(a.never, function (err) { a(err, e, "Again B: Result"); }); setTimeout(function (err) { a(i, 4, "Again Called #2"); d(); }, 10); }, 10); } }, "Cancellation": { Immediate: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; var p = new Bluebird(function (res) { setTimeout(function () { res(x + y); }, 100); }); p.cancel(); return p; }; mfn = memoize(fn, { promise: true }); mfn(3, 7).done(a.never, function (err) { a.throws(function () { throw err; }, Bluebird.CancellationError, "Result #1"); }); mfn(5, 8).done(a.never, function (err) { a.throws(function () { throw err; }, Bluebird.CancellationError, "Result B #2"); }); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(a.never, function (err) { a.throws(function () { throw err; }, Bluebird.CancellationError, "Again: Result"); }); mfn(5, 8).done(a.never, function (err) { a.throws(function () { throw err; }, Bluebird.CancellationError, "Again B: Result"); }); setTimeout(function (err) { a(i, 4, "Again Called #2"); d(); }, 10); }, 10); }, Delayed: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; var p = new Bluebird(function (res) { setTimeout(function () { res(x + y); }, 100); }); nextTick(function () { p.cancel(); }, 1); return p; }; mfn = memoize(fn, { promise: true }); mfn(3, 7).done(a.never, a.never); mfn(5, 8).done(a.never, a.never); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(a.never, a.never); mfn(5, 8).done(a.never, a.never); setTimeout(function (err) { a(i, 4, "Again Called #2"); d(); }, 500); }, 500); } }, "Primitive": { "Success": function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { return new Promise(function (res) { ++i; res(x + y); }); }; mfn = memoize(fn, { promise: true, primitive: true }); mfn(3, 7).done(function (res) { a(res, 10, "Result #1"); }, a.never); mfn(3, 7).done(function (res) { a(res, 10, "Result #2"); }, a.never); mfn(5, 8).done(function (res) { a(res, 13, "Result B #1"); }, a.never); mfn(3, 7).done(function (res) { a(res, 10, "Result #3"); }, a.never); mfn(5, 8).done(function (res) { a(res, 13, "Result B #2"); }, a.never); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }, a.never); mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }, a.never); setTimeout(function () { a(i, 2, "Again Called #2"); mfn.delete(3, 7); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }, a.never); mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }, a.never); setTimeout(function () { a(i, 3, "Call After delete"); d(); }, 10); }, 10); }, 10); }, "Error": function (a, d) { var mfn, fn, i = 0, e = new Error("Test"); fn = function (x, y) { return new Promise(function (res, rej) { ++i; rej(e); }); }; mfn = memoize(fn, { promise: "done", primitive: true }); mfn(3, 7).done(a.never, function (err) { a(err, e, "Result #1"); }); mfn(5, 8).done(a.never, function (err) { a(err, e, "Result B #2"); }); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(a.never, function (err) { a(err, e, "Again: Result"); }); mfn(5, 8).done(a.never, function (err) { a(err, e, "Again B: Result"); }); setTimeout(function (err) { a(i, 4, "Again Called #2"); d(); }, 10); }, 10); }, "Primitive null arg case": function (a, d) { var mfn, x = {}; mfn = memoize( function f(id) { return new Promise(function (res) { res(x); }); }, { promise: true, primitive: true } ); mfn(null).done(function (res) { a.deep(res, x, "Args"); d(); }, a.never); } }, "Sync Clear": function (a, d) { var mfn, fn; fn = function (x) { return new Promise(function (res) { nextTick(function () { res(x); }); }); }; mfn = memoize(fn, { promise: true }); mfn(1).done(function (res) { a(res, 1, "First"); }, a.never); mfn(2).done(function (res) { a(res, 2, "Second"); d(); }, a.never); }, "Sync Clear: Primitive": function (a, d) { var mfn, fn; fn = function (x) { return new Promise(function (res) { nextTick(function () { res(x); }); }); }; mfn = memoize(fn, { promise: true, primitive: true }); mfn(1).done(function (res) { a(res, 1, "First"); }, a.never); mfn(2).done(function (res) { a(res, 2, "Second"); d(); }, a.never); } }; }; memoizee-0.4.15/test/ext/ref-counter.js000066400000000000000000000177711377604527300200140ustar00rootroot00000000000000/* eslint max-lines: 0, id-length: 0, no-undef: 0 */ "use strict"; var memoize = require("../..") , nextTick = require("next-tick") , Promise = require("plain-promise"); module.exports = function () { return { "Regular": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return x + y + z; } , mfn; mfn = memoize(fn, { refCounter: true }); a(mfn.deleteRef(3, 5, 7), null, "Delete before"); a(mfn(3, 5, 7), 15, "Initial"); a(mfn(3, 5, 7), 15, "Cache"); a(mfn.deleteRef(3, 5, 7), false, "Delete #1"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Delete #2"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Delete #3"); mfn(3, 5, 7); a(i, 1, "Not deleteed"); a(mfn.deleteRef(3, 5, 7), false, "Delete #4"); a(mfn.deleteRef(3, 5, 7), true, "Delete final"); mfn(3, 5, 7); a(i, 2, "Restarted"); mfn(3, 5, 7); a(i, 2, "Cached again"); }, "Regular: Async": function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, refCounter: true }); a(mfn.deleteRef(3, 7), null, "Delete ref before"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Again Called #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #1"); a(mfn.deleteRef(3, 7), false, "Delete ref #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #3"); a(mfn.deleteRef(3, 7), true, "Delete ref Final"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Call After delete"); d(); }); }); }); }, "Regular: Promise": function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { promise: true, refCounter: true }); a(mfn.deleteRef(3, 7), null, "Delete ref before"); mfn(3, 7).done(function (res) { a(res, 10, "Result #1"); }); mfn(3, 7).done(function (res) { a(res, 10, "Result #2"); }); mfn(5, 8).done(function (res) { a(res, 13, "Result B #1"); }); mfn(3, 7).done(function (res) { a(res, 10, "Result #3"); }); mfn(5, 8).done(function (res) { a(res, 13, "Result B #2"); }); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); setTimeout(function () { a(i, 2, "Again Called #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #1"); a(mfn.deleteRef(3, 7), false, "Delete ref #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #3"); a(mfn.deleteRef(3, 7), true, "Delete ref Final"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); setTimeout(function () { a(i, 3, "Call After delete"); d(); }, 10); }, 10); }, 10); }, "Primitive": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return x + y + z; } , mfn; mfn = memoize(fn, { primitive: true, refCounter: true }); a(mfn.deleteRef(3, 5, 7), null, "Delete before"); a(mfn(3, 5, 7), 15, "Initial"); a(mfn(3, 5, 7), 15, "Cache"); a(mfn.deleteRef(3, 5, 7), false, "Delete #1"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Delete #2"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Delete #3"); mfn(3, 5, 7); a(i, 1, "Not deleteed"); a(mfn.deleteRef(3, 5, 7), false, "Delete #4"); a(mfn.deleteRef(3, 5, 7), true, "Delete final"); mfn(3, 5, 7); a(i, 2, "Restarted"); mfn(3, 5, 7); a(i, 2, "Cached again"); }, "Primitive: Async": function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = memoize(fn, { async: true, primitive: true, refCounter: true }); a(mfn.deleteRef(3, 7), null, "Delete ref before"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Again Called #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #1"); a(mfn.deleteRef(3, 7), false, "Delete ref #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #3"); a(mfn.deleteRef(3, 7), true, "Delete ref Final"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Call After delete"); d(); }); }); }); }, "Primitive: Promise": function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return new Promise(function (res) { res(x + y); }); }; mfn = memoize(fn, { promise: true, primitive: true, refCounter: true }); a(mfn.deleteRef(3, 7), null, "Delete ref before"); mfn(3, 7).done(function (res) { a(res, 10, "Result #1"); }); mfn(3, 7).done(function (res) { a(res, 10, "Result #2"); }); mfn(5, 8).done(function (res) { a(res, 13, "Result B #1"); }); mfn(3, 7).done(function (res) { a(res, 10, "Result #3"); }); mfn(5, 8).done(function (res) { a(res, 13, "Result B #2"); }); setTimeout(function () { a(i, 2, "Called #2"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); setTimeout(function () { a(i, 2, "Again Called #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #1"); a(mfn.deleteRef(3, 7), false, "Delete ref #2"); a(mfn.deleteRef(3, 7), false, "Delete ref #3"); a(mfn.deleteRef(3, 7), true, "Delete ref Final"); mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); setTimeout(function () { a(i, 3, "Call After delete"); d(); }, 10); }, 10); }, 10); } }; }; memoizee-0.4.15/test/index.js000066400000000000000000002047361377604527300160710ustar00rootroot00000000000000/* eslint max-lines: 0, max-statements: 0, id-length: 0, no-shadow: 0, max-nested-callbacks: 0, max-len: 0 */ "use strict"; var aFrom = require("es5-ext/array/from") , nextTick = require("next-tick"); var join = Array.prototype.join; module.exports = function (t, a) { return { "0": function () { var i = 0, fn = function () { ++i; return 3; }; fn = t(fn); a(fn(), 3, "First"); a(fn(1), 3, "Second"); a(fn(5), 3, "Third"); a(i, 1, "Called once"); }, "1": function () { var i = 0, fn = function (x) { ++i; return x; }; fn = t(fn); return { "No arg": function () { i = 0; a(fn(), undefined, "First"); a(fn(), undefined, "Second"); a(fn(), undefined, "Third"); a(i, 1, "Called once"); }, "Arg": function () { var x = {}; i = 0; a(fn(x, 8), x, "First"); a(fn(x, 4), x, "Second"); a(fn(x, 2), x, "Third"); a(i, 1, "Called once"); }, "Other Arg": function () { var x = {}; i = 0; a(fn(x, 2), x, "First"); a(fn(x, 9), x, "Second"); a(fn(x, 3), x, "Third"); a(i, 1, "Called once"); } }; }, "3": function () { var i = 0, fn = function (x, y, z) { ++i; return [x, y, z]; }, r; fn = t(fn); return { "No args": function () { i = 0; a.deep(r = fn(), [undefined, undefined, undefined], "First"); a(fn(), r, "Second"); a(fn(), r, "Third"); a(i, 1, "Called once"); }, "Some Args": function () { var x = {}; i = 0; a.deep(r = fn(x, 8), [x, 8, undefined], "First"); a(fn(x, 8), r, "Second"); a(fn(x, 8), r, "Third"); a(i, 1, "Called once"); return { Other: function () { a.deep(r = fn(x, 5), [x, 5, undefined], "Second"); a(fn(x, 5), r, "Third"); a(i, 2, "Called once"); } }; }, "Full stuff": function () { var x = {}; i = 0; a.deep(r = fn(x, 8, 23, 98), [x, 8, 23], "First"); a(fn(x, 8, 23, 43), r, "Second"); a(fn(x, 8, 23, 9), r, "Third"); a(i, 1, "Called once"); return { Other: function () { a.deep(r = fn(x, 23, 8, 13), [x, 23, 8], "Second"); a(fn(x, 23, 8, 22), r, "Third"); a(i, 2, "Called once"); } }; } }; }, "Error": function () { var i = 0, e = new Error("Test"), fn = function () { ++i; if (i < 3) throw e; return 3; }; fn = t(fn); a.throws(function () { fn(); }, "Test"); a(i, 1, "Called once"); a.throws(function () { fn(); }, "Test"); a(i, 2, "Called twice"); a(fn(), 3, "First"); a(fn(), 3, "Second"); a(fn(), 3, "Third"); a(i, 3, "Called three times"); }, "Normalizer function": function () { var i = 0, fn = function () { ++i; return join.call(arguments, "|"); }, mfn; mfn = t(fn, { normalizer: function (args) { return Boolean(args[0]); } }); a(mfn(false, "raz"), "false|raz", "#1"); a(mfn(0, "dwa"), "false|raz", "#2"); a(i, 1, "Called once"); a(mfn(34, "bar"), "34|bar", "#3"); a(i, 2, "Called twice"); a(mfn(true, "ola"), "34|bar", "#4"); a(i, 2, "Called twice #2"); }, "Dynamic": function () { var i = 0, fn = function () { ++i; return arguments; }, r; fn = t(fn, { length: false }); return { "No args": function () { i = 0; a.deep(aFrom(r = fn()), [], "First"); a(fn(), r, "Second"); a(fn(), r, "Third"); a(i, 1, "Called once"); }, "Some Args": function () { var x = {}; i = 0; a.deep(aFrom(r = fn(x, 8)), [x, 8], "First"); a(fn(x, 8), r, "Second"); a(fn(x, 8), r, "Third"); a(i, 1, "Called once"); }, "Many args": function () { var x = {}; i = 0; a.deep(aFrom(r = fn(x, 8, 23, 98)), [x, 8, 23, 98], "First"); a(fn(x, 8, 23, 98), r, "Second"); a(fn(x, 8, 23, 98), r, "Third"); a(i, 1, "Called once"); } }; }, "Resolvers": function () { var i = 0, fn, r; fn = t( function () { ++i; return arguments; }, { length: 3, resolvers: [Boolean, String] } ); return { "No args": function () { i = 0; a.deep(aFrom(r = fn()), [false, "undefined"], "First"); a(fn(), r, "Second"); a(fn(), r, "Third"); a(i, 1, "Called once"); }, "Some Args": function () { var x = {}; i = 0; a.deep(aFrom(r = fn(0, 34, x, 45)), [false, "34", x, 45], "First"); a(fn(0, 34, x, 22), r, "Second"); a(fn(0, 34, x, false), r, "Third"); a(i, 1, "Called once"); return { Other: function () { a.deep(aFrom(r = fn(1, 34, x, 34)), [true, "34", x, 34], "Second"); a(fn(1, 34, x, 89), r, "Third"); a(i, 2, "Called once"); } }; } }; }, "Clear Cache": { Specific: function () { var i = 0, fn, mfn, x = {}; fn = function (a, b, c) { if (c === 3) { ++i; } return arguments; }; mfn = t(fn); mfn(1, x, 3); mfn(1, x, 4); mfn.delete(1, x, 4); mfn(1, x, 3); mfn(1, x, 3); a(i, 1, "Pre clear"); mfn.delete(1, x, 3); mfn(1, x, 3); a(i, 2, "After clear"); i = 0; mfn = t(fn, { length: false }); mfn(1, x, 3); mfn(1, x, 3); mfn(); mfn(); mfn.delete(); mfn(1, x, 3); a(i, 1, "Proper no arguments clear"); }, All: function () { var i = 0, fn, x = {}; fn = function () { ++i; return arguments; }; fn = t(fn, { length: 3 }); fn(1, x, 3); fn(1, x, 4); fn(1, x, 3); fn(1, x, 4); a(i, 2, "Pre clear"); fn.clear(); fn(1, x, 3); fn(1, x, 4); fn(1, x, 3); fn(1, x, 4); a(i, 4, "After clear"); } }, "Primitive": { "No args": function (a) { var i = 0, fn = function () { ++i; return arguments[0]; }, mfn; mfn = t(fn, { primitive: true }); a(mfn("ble"), "ble", "#1"); a(mfn({}), "ble", "#2"); a(i, 1, "Called once"); }, "One arg": function (a) { var i = 0, fn = function (x) { ++i; return x; }, mfn, y = { toString: function () { return "foo"; } }; mfn = t(fn, { primitive: true }); a(mfn(y), y, "#1"); a(mfn("foo"), y, "#2"); a(i, 1, "Called once"); }, "Many args": function (a) { var i = 0, fn = function (x, y, z) { ++i; return x + y + z; }, mfn, y = { toString: function () { return "foo"; } }; mfn = t(fn, { primitive: true }); a(mfn(y, "bar", "zeta"), "foobarzeta", "#1"); a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2"); a(i, 1, "Called once"); }, "Clear cache": function (a) { var i = 0, fn = function (x, y, z) { ++i; return x + y + z; }, mfn, y = { toString: function () { return "foo"; } }; mfn = t(fn, { primitive: true }); a(mfn(y, "bar", "zeta"), "foobarzeta", "#1"); a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2"); a(i, 1, "Called once"); mfn.delete( "foo", { toString: function () { return "bar"; } }, "zeta" ); a(mfn(y, "bar", "zeta"), "foobarzeta", "#3"); a(i, 2, "Called twice"); } }, "Reference counter": { Regular: function (a) { var i = 0, fn = function (x, y, z) { ++i; return x + y + z; }, mfn; mfn = t(fn, { refCounter: true }); a(mfn.deleteRef(3, 5, 7), null, "Clear before"); a(mfn(3, 5, 7), 15, "Initial"); a(mfn(3, 5, 7), 15, "Cache"); a(mfn.deleteRef(3, 5, 7), false, "Clear #1"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Clear #2"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Clear #3"); mfn(3, 5, 7); a(i, 1, "Not cleared"); a(mfn.deleteRef(3, 5, 7), false, "Clear #4"); a(mfn.deleteRef(3, 5, 7), true, "Clear final"); mfn(3, 5, 7); a(i, 2, "Restarted"); mfn(3, 5, 7); a(i, 2, "Cached again"); }, Primitive: function (a) { var i = 0, fn = function (x, y, z) { ++i; return x + y + z; }, mfn; mfn = t(fn, { primitive: true, refCounter: true }); a(mfn.deleteRef(3, 5, 7), null, "Clear before"); a(mfn(3, 5, 7), 15, "Initial"); a(mfn(3, 5, 7), 15, "Cache"); a(mfn.deleteRef(3, 5, 7), false, "Clear #1"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Clear #2"); mfn(3, 5, 7); a(mfn.deleteRef(3, 5, 7), false, "Clear #3"); mfn(3, 5, 7); a(i, 1, "Not cleared"); a(mfn.deleteRef(3, 5, 7), false, "Clear #4"); a(mfn.deleteRef(3, 5, 7), true, "Clear final"); mfn(3, 5, 7); a(i, 2, "Restarted"); mfn(3, 5, 7); a(i, 2, "Cached again"); } }, "Async": { Regular: { "Success": function (a, d) { var mfn, fn, u = {}, i = 0, invoked = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true }); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Init Called"); a(invoked, 5, "Cb Called"); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Init Called #2"); a(invoked, 7, "Cb Called #2"); mfn.delete(3, 7); a( mfn(3, 7, function (err, res) { ++invoked; a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { ++invoked; a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Init After clear"); a(invoked, 9, "Cb After clear"); d(); }); }); }); }, "Reference counter": function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, refCounter: true }); a(mfn.deleteRef(3, 7), null, "Clear ref before"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Again Called #2"); a(mfn.deleteRef(3, 7), false, "Clear ref #1"); a(mfn.deleteRef(3, 7), false, "Clear ref #2"); a(mfn.deleteRef(3, 7), false, "Clear ref #3"); a(mfn.deleteRef(3, 7), true, "Clear ref Final"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Call After clear"); d(); }); }); }); }, "Error": function (a, d) { var mfn, fn, u = {}, i = 0, e = new Error("Test"); fn = function (x, y, cb) { nextTick(function () { ++i; cb(e); }); return u; }; mfn = t(fn, { async: true }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Again Called #2"); d(); }); }); } }, Primitive: { "Success": function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, primitive: true }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Again Called #2"); mfn.delete(3, 7); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Call After clear"); d(); }); }); }); }, "Reference counter": function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, primitive: true, refCounter: true }); a(mfn.deleteRef(3, 7), null, "Clear ref before"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 2, "Again Called #2"); a(mfn.deleteRef(3, 7), false, "Clear ref #1"); a(mfn.deleteRef(3, 7), false, "Clear ref #2"); a(mfn.deleteRef(3, 7), false, "Clear ref #3"); a(mfn.deleteRef(3, 7), true, "Clear ref Final"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 3, "Call After clear"); d(); }); }); }); }, "Error": function (a, d) { var mfn, fn, u = {}, i = 0, e = new Error("Test"); fn = function (x, y, cb) { nextTick(function () { ++i; cb(e); }); return u; }; mfn = t(fn, { async: true, primitive: true }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Result B #2"); }), u, "Initial #3" ); nextTick(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [e, undefined], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [e, undefined], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Again Called #2"); d(); }); }); } } }, "MaxAge": { Regular: { Sync: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = t(fn, { maxAge: 100 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait"); a(i, 2, "Called: Wait"); a(mfn(5, 8), 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait After"); a(i, 3, "Called: Wait After"); a(mfn(5, 8), 13, "Result: Wait After B"); a(i, 4, "Called: Wait After B"); a(mfn(3, 7), 10, "Result: Wait After #2"); a(i, 4, "Called: Wait After #2"); a(mfn(5, 8), 13, "Result: Wait After B #2"); a(i, 4, "Called: Wait After B #2"); d(); }, 100); }, 20); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, maxAge: 100 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); setTimeout(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); setTimeout(function () { a(i, 2, "Again Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Call After clear"); d(); }); }, 100); }, 20); } }, Primitive: { Sync: function (a, d) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = t(fn, { primitive: true, maxAge: 100 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait"); a(i, 2, "Called: Wait"); a(mfn(5, 8), 13, "Result: Wait B"); a(i, 2, "Called: Wait B"); setTimeout(function () { a(mfn(3, 7), 10, "Result: Wait After"); a(i, 3, "Called: Wait After"); a(mfn(5, 8), 13, "Result: Wait After B"); a(i, 4, "Called: Wait After B"); a(mfn(3, 7), 10, "Result: Wait After #2"); a(i, 4, "Called: Wait After #2"); a(mfn(5, 8), 13, "Result: Wait After B #2"); a(i, 4, "Called: Wait After B #2"); d(); }, 100); }, 20); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, primitive: true, maxAge: 100 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, "Initial" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), u, "Initial #2" ); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, "Initial #2" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), u, "Initial #3" ); setTimeout(function () { a(i, 2, "Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); setTimeout(function () { a(i, 2, "Again Called #2"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Again: Result"); }), u, "Again: Initial" ); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Again B: Result"); }), u, "Again B: Initial" ); nextTick(function () { a(i, 4, "Call After clear"); d(); }); }, 100); }, 20); } } }, "Max": { Regular: { Sync: function (a) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = t(fn, { max: 3 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); a(mfn(12, 4), 16, "Result C #1"); a(i, 3, "Called C #1"); a(mfn(3, 7), 10, "Result #4"); a(i, 3, "Called #4"); a(mfn(5, 8), 13, "Result B #3"); a(i, 3, "Called B #3"); a(mfn(77, 11), 88, "Result D #1"); // Clear 12, 4 a(i, 4, "Called D #1"); a(mfn(5, 8), 13, "Result B #4"); a(i, 4, "Called B #4"); a(mfn(12, 4), 16, "Result C #2"); // Clear 3, 7 a(i, 5, "Called C #2"); a(mfn(3, 7), 10, "Result #5"); // Clear 77, 11 a(i, 6, "Called #5"); a(mfn(77, 11), 88, "Result D #2"); // Clear 5, 8 a(i, 7, "Called D #2"); a(mfn(12, 4), 16, "Result C #3"); a(i, 7, "Called C #3"); a(mfn(5, 8), 13, "Result B #5"); // Clear 3, 7 a(i, 8, "Called B #5"); a(mfn(77, 11), 88, "Result D #3"); a(i, 8, "Called D #3"); mfn.delete(77, 11); a(mfn(77, 11), 88, "Result D #4"); a(i, 9, "Called D #4"); mfn.clear(); a(mfn(5, 8), 13, "Result B #6"); a(i, 10, "Called B #6"); a(mfn(77, 11), 88, "Result D #5"); a(i, 11, "Called D #5"); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, max: 3 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); a(i, 1, "Called #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); a(i, 1, "Called #2"); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); a(i, 2, "Called B #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); a(i, 2, "Called #3"); a( mfn(5, 8, function (err, res) { a.deep( [err, res], [null, 13], "Result B #2" ); a(i, 2, "Called B #2"); a( mfn(12, 4, function (err, res) { a.deep( [err, res], [null, 16], "Result C #1" ); a(i, 3, "Called C #1"); a( mfn(3, 7, function ( err, res ) { a.deep( [err, res], [null, 10], "Result #4" ); a(i, 3, "Called #4"); a( mfn(5, 8, function ( err, res ) { a.deep( [err, res], [null, 13], "Result B #3" ); a( i, 3, "Called B #3" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #1" ); a( i, 4, "Called D #1" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #4" ); a( i, 4, "Called B #4" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #2" ); a( i, 5, "Called C #2" ); a( mfn( 3, 7, function ( err, res ) { a.deep( [ err, res ], [ null, 10 ], "Result #5" ); a( i, 6, "Called #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #2" ); a( i, 7, "Called D #2" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #3" ); a( i, 7, "Called C #3" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #5" ); a( i, 8, "Called B #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #3" ); a( i, 8, "Called D #3" ); mfn.delete( 77, 11 ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #4" ); a( i, 9, "Called D #4" ); mfn.clear(); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #6" ); a( i, 10, "Called B #6" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #5" ); a( i, 11, "Called D #5" ); d(); } ), u, "Initial D #5" ); } ), u, "Initial B #6" ); } ), u, "Initial D #4" ); } ), u, "Initial D #3" ); } ), u, "Initial B #5" ); } ), u, "Initial C #3" ); } ), u, "Initial D #2" ); } ), u, "Initial #5" ); } ), u, "Initial C #2" ); } ), u, "Initial B #4" ); } ), u, "Initial D #1" ); }), u, "Initial B #3" ); }), u, "Initial #4" ); }), u, "Initial C #1" ); }), u, "Initial B #2" ); }), u, "Initial #3" ); }), u, "Initial B #1" ); }), u, "Initial #2" ); }), u, "Initial #1" ); } }, Primitive: { Sync: function (a) { var mfn, fn, i = 0; fn = function (x, y) { ++i; return x + y; }; mfn = t(fn, { primitive: true, max: 3 }); a(mfn(3, 7), 10, "Result #1"); a(i, 1, "Called #1"); a(mfn(3, 7), 10, "Result #2"); a(i, 1, "Called #2"); a(mfn(5, 8), 13, "Result B #1"); a(i, 2, "Called B #1"); a(mfn(3, 7), 10, "Result #3"); a(i, 2, "Called #3"); a(mfn(5, 8), 13, "Result B #2"); a(i, 2, "Called B #2"); a(mfn(12, 4), 16, "Result C #1"); a(i, 3, "Called C #1"); a(mfn(3, 7), 10, "Result #4"); a(i, 3, "Called #4"); a(mfn(5, 8), 13, "Result B #3"); a(i, 3, "Called B #3"); a(mfn(77, 11), 88, "Result D #1"); // Clear 12, 4 a(i, 4, "Called D #1"); a(mfn(5, 8), 13, "Result B #4"); a(i, 4, "Called B #4"); a(mfn(12, 4), 16, "Result C #2"); // Clear 3, 7 a(i, 5, "Called C #2"); a(mfn(3, 7), 10, "Result #5"); // Clear 77, 11 a(i, 6, "Called #5"); a(mfn(77, 11), 88, "Result D #2"); // Clear 5, 8 a(i, 7, "Called D #2"); a(mfn(12, 4), 16, "Result C #3"); a(i, 7, "Called C #3"); a(mfn(5, 8), 13, "Result B #5"); // Clear 3, 7 a(i, 8, "Called B #5"); a(mfn(77, 11), 88, "Result D #3"); a(i, 8, "Called D #3"); mfn.delete(77, 11); a(mfn(77, 11), 88, "Result D #4"); a(i, 9, "Called D #4"); mfn.clear(); a(mfn(5, 8), 13, "Result B #6"); a(i, 10, "Called B #6"); a(mfn(77, 11), 88, "Result D #5"); a(i, 11, "Called D #5"); }, Async: function (a, d) { var mfn, fn, u = {}, i = 0; fn = function (x, y, cb) { nextTick(function () { ++i; cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, primitive: true, max: 3 }); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); a(i, 1, "Called #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); a(i, 1, "Called #2"); a( mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); a(i, 2, "Called B #1"); a( mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); a(i, 2, "Called #3"); a( mfn(5, 8, function (err, res) { a.deep( [err, res], [null, 13], "Result B #2" ); a(i, 2, "Called B #2"); a( mfn(12, 4, function (err, res) { a.deep( [err, res], [null, 16], "Result C #1" ); a(i, 3, "Called C #1"); a( mfn(3, 7, function ( err, res ) { a.deep( [err, res], [null, 10], "Result #4" ); a(i, 3, "Called #4"); a( mfn(5, 8, function ( err, res ) { a.deep( [err, res], [null, 13], "Result B #3" ); a( i, 3, "Called B #3" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #1" ); a( i, 4, "Called D #1" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #4" ); a( i, 4, "Called B #4" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #2" ); a( i, 5, "Called C #2" ); a( mfn( 3, 7, function ( err, res ) { a.deep( [ err, res ], [ null, 10 ], "Result #5" ); a( i, 6, "Called #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #2" ); a( i, 7, "Called D #2" ); a( mfn( 12, 4, function ( err, res ) { a.deep( [ err, res ], [ null, 16 ], "Result C #3" ); a( i, 7, "Called C #3" ); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #5" ); a( i, 8, "Called B #5" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #3" ); a( i, 8, "Called D #3" ); mfn.delete( 77, 11 ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #4" ); a( i, 9, "Called D #4" ); mfn.clear(); a( mfn( 5, 8, function ( err, res ) { a.deep( [ err, res ], [ null, 13 ], "Result B #6" ); a( i, 10, "Called B #6" ); a( mfn( 77, 11, function ( err, res ) { a.deep( [ err, res ], [ null, 88 ], "Result D #5" ); a( i, 11, "Called D #5" ); d(); } ), u, "Initial D #5" ); } ), u, "Initial B #6" ); } ), u, "Initial D #4" ); } ), u, "Initial D #3" ); } ), u, "Initial B #5" ); } ), u, "Initial C #3" ); } ), u, "Initial D #2" ); } ), u, "Initial #5" ); } ), u, "Initial C #2" ); } ), u, "Initial B #4" ); } ), u, "Initial D #1" ); }), u, "Initial B #3" ); }), u, "Initial #4" ); }), u, "Initial C #1" ); }), u, "Initial B #2" ); }), u, "Initial #3" ); }), u, "Initial B #1" ); }), u, "Initial #2" ); }), u, "Initial #1" ); } } }, "Dispose": { Regular: { "Sync": function (a) { var mfn, fn, value = [], x, invoked; fn = function (x, y) { return x + y; }; mfn = t(fn, { dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); x = {}; invoked = false; mfn = t( function () { return x; }, { dispose: function (val) { invoked = val; } } ); mfn.delete(); a(invoked, false, "No args: Post invalid clear"); mfn(); a(invoked, false, "No args: Post cache"); mfn.delete(); a(invoked, x, "No args: Pre clear"); }, "Ref counter": function (a) { var mfn, fn, value = []; fn = function (x, y) { return x + y; }; mfn = t(fn, { refCounter: true, dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn(5, 8); mfn.deleteRef(5, 8); a.deep(value, [], "Pre"); mfn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; mfn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); }, "Async": function (a, d) { var mfn, fn, u = {}, value = []; fn = function (x, y, cb) { nextTick(function () { cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, dispose: function (val) { value.push(val); } }); mfn(3, 7, function () { mfn(5, 8, function () { mfn(12, 4, function () { a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11, function () { mfn.clear(); a.deep(value, [10, 88], "Clear all"); d(); }); }); }); }); } }, Primitive: { "Sync": function (a) { var mfn, fn, value = []; fn = function (x, y) { return x + y; }; mfn = t(fn, { dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); }, "Ref counter": function (a) { var mfn, fn, value = []; fn = function (x, y) { return x + y; }; mfn = t(fn, { refCounter: true, dispose: function (val) { value.push(val); } }); mfn(3, 7); mfn(5, 8); mfn(12, 4); a.deep(value, [], "Pre"); mfn(5, 8); mfn.deleteRef(5, 8); a.deep(value, [], "Pre"); mfn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; mfn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11); mfn.clear(); a.deep(value, [10, 88], "Clear all"); }, "Async": function (a, d) { var mfn, fn, u = {}, value = []; fn = function (x, y, cb) { nextTick(function () { cb(null, x + y); }); return u; }; mfn = t(fn, { async: true, dispose: function (val) { value.push(val); } }); mfn(3, 7, function () { mfn(5, 8, function () { mfn(12, 4, function () { a.deep(value, [], "Pre"); mfn.delete(5, 8); a.deep(value, [13], "#1"); value = []; mfn.delete(12, 4); a.deep(value, [16], "#2"); value = []; mfn(77, 11, function () { mfn.clear(); a.deep(value, [10, 88], "Clear all"); d(); }); }); }); }); } } } }; }; memoizee-0.4.15/test/lib/000077500000000000000000000000001377604527300151565ustar00rootroot00000000000000memoizee-0.4.15/test/lib/configure-map.js000066400000000000000000000061411377604527300202520ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; var aFrom = require("es5-ext/array/from") , memoize = require("../.."); module.exports = function () { return { "One arg": function (a) { var i = 0 , fn = function (x) { ++i; return x; } , mfn , y = { toString: function () { return "foo"; } }; mfn = memoize(fn, { primitive: true }); a(mfn(y), y, "#1"); a(mfn("foo"), y, "#2"); a(i, 1, "Called once"); }, "No args": function (a) { var i = 0 , fn = function () { ++i; return "foo"; } , mfn , y = { toString: function () { return "foo"; } }; mfn = memoize(fn); a(mfn._has(), false); a(mfn(), "foo", "#1"); a(mfn._has(), true); mfn(); a(i, 1, "Called once"); }, "Clear cache": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return x + y + z; } , mfn , y = { toString: function () { return "foo"; } }; mfn = memoize(fn, { primitive: true }); a(mfn(y, "bar", "zeta"), "foobarzeta", "#1"); a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2"); a(i, 1, "Called once"); mfn.delete( "foo", { toString: function () { return "bar"; } }, "zeta" ); a(mfn(y, "bar", "zeta"), "foobarzeta", "#3"); a(i, 2, "Called twice"); }, "_get": function (a) { var fn = function (x) { return x; } , mfn; mfn = memoize(fn); a(mfn._get("foo"), undefined); mfn("foo"); a(mfn._get("foo"), "foo"); }, "_has": function (a) { var fn = function (x) { return x; } , mfn; mfn = memoize(fn); a(mfn._has("foo"), false); mfn("foo"); a(mfn._has("foo"), true); }, "Circular": function (a) { var i = 0, fn; fn = memoize(function (x) { if (++i < 2) fn(x); }); a.throws(function () { fn("foo"); }, "CIRCULAR_INVOCATION"); i = 0; fn = memoize(function (x, y) { if (++i < 2) fn(x, y); }); a.throws(function () { fn("foo", "bar"); }, "CIRCULAR_INVOCATION"); }, "Resolvers": function () { var i = 0, fn, r; fn = memoize( function () { ++i; return arguments; }, { length: 3, resolvers: [Boolean, String] } ); return { "No args": function (a) { i = 0; a.deep(aFrom(r = fn()), [false, "undefined"], "First"); a(fn(), r, "Second"); a(fn(), r, "Third"); a(i, 1, "Called once"); }, "One arg": function (a) { var fn = memoize( function (elo) { ++i; return arguments; }, { resolvers: [Boolean] } ); a.deep(aFrom(r = fn("elo")), [true], "First"); }, "Some Args": function (a) { var x = {}; i = 0; a.deep(aFrom(r = fn(0, 34, x, 45)), [false, "34", x, 45], "First"); a(fn(0, 34, x, 22), r, "Second"); a(fn(0, 34, x, false), r, "Third"); a(i, 1, "Called once"); return { Other: function (a) { a.deep(aFrom(r = fn(1, 34, x, 34)), [true, "34", x, 34], "Second"); a(fn(1, 34, x, 89), r, "Third"); a(i, 2, "Called once"); } }; } }; } }; }; memoizee-0.4.15/test/lib/methods.js000066400000000000000000000015601377604527300171610ustar00rootroot00000000000000/* eslint id-length: 0 */ "use strict"; var d = require("d") , memoize = require("../.."); require("../ext/dispose"); require("../ext/ref-counter"); module.exports = function (t, a) { var value = [], obj = {}; t = t(memoize); Object.defineProperties( obj, t({ someFn: d( function (x, y) { a(this, obj); return x + y; }, { refCounter: true, dispose: function (val) { value.push(val); } } ) }) ); obj = Object.create(obj); obj.someFn(3, 7); obj.someFn(5, 8); obj.someFn(12, 4); a.deep(value, [], "Pre"); obj.someFn(5, 8); obj.someFn.deleteRef(5, 8); a.deep(value, [], "Pre"); obj.someFn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; obj.someFn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; obj.someFn(77, 11); obj.someFn.clear(); a.deep(value, [10, 88], "Clear"); }; memoizee-0.4.15/test/lib/registered-extensions.js000066400000000000000000000001621377604527300220450ustar00rootroot00000000000000"use strict"; module.exports = function (t, a) { require("../../ext/async"); a(typeof t.async, "function"); }; memoizee-0.4.15/test/lib/resolve-length.js000066400000000000000000000004701377604527300204530ustar00rootroot00000000000000"use strict"; module.exports = function (t, a) { a(t(1, 2), 1, "Options"); a(t(1, 2, true), 1, "Options: Async "); a(t(undefined, 2), 2, "Function"); a(t(undefined, 2, true), 1, "Function: Async"); a(t(undefined, undefined, false), 1, "Unknown"); a(t(undefined, undefined, true), 1, "Unknown: async"); }; memoizee-0.4.15/test/lib/resolve-normalize.js000066400000000000000000000003311377604527300211660ustar00rootroot00000000000000/* eslint no-empty-function: 0 */ "use strict"; module.exports = function (t, a) { var fn = function () {}, resolved = t(fn); a.deep(resolved, { get: fn, set: fn }); a.deep(t(resolved), { get: fn, set: fn }); }; memoizee-0.4.15/test/lib/resolve-resolve.js000066400000000000000000000002151377604527300206460ustar00rootroot00000000000000"use strict"; module.exports = function (t, a) { a.deep(t([String, null, Number])([23, "foo", "45", "elo"]), ["23", "foo", 45, "elo"]); }; memoizee-0.4.15/test/lib/weak.js000066400000000000000000000017451377604527300164520ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; var memoize = require("../.."); require("../ext/dispose"); require("../ext/ref-counter"); module.exports = function (t, a) { var value = [], obj = {}, memoized, count = 0, x, y, z; t = t(memoize); memoized = t( function (arg, x, y) { a(arg, obj); return x + y; }, { refCounter: true, dispose: function (val) { value.push(val); } } ); a(memoized(obj, 3, 7), 10); a(memoized(obj, 5, 8), 13); a(memoized(obj, 12, 4), 16); a.deep(value, [], "Pre"); a(memoized(obj, 5, 8), 13); memoized.deleteRef(obj, 5, 8); a.deep(value, [], "Pre"); memoized.deleteRef(obj, 5, 8); a.deep(value, [13], "#1"); value = []; memoized.deleteRef(obj, 12, 4); a.deep(value, [16], "#2"); value = []; memoized(obj, 77, 11); x = {}; y = {}; z = {}; memoized = t(function (arg) { return ++count; }); a(memoized(x), 1); a(memoized(y), 2); a(memoized(x), 1); a(memoized(z), 3); a(count, 3); }; memoizee-0.4.15/test/methods-plain.js000066400000000000000000000014771377604527300175230ustar00rootroot00000000000000/* eslint id-length: 0 */ "use strict"; var d = require("d"); require("../ext/dispose"); require("../ext/ref-counter"); module.exports = function (t, a) { var value = [], obj = {}; Object.defineProperties( obj, t({ someFn: d( function (x, y) { a(this, obj); return x + y; }, { refCounter: true, dispose: function (val) { value.push(val); } } ) }) ); obj = Object.create(obj); obj.someFn(3, 7); obj.someFn(5, 8); obj.someFn(12, 4); a.deep(value, [], "Pre"); obj.someFn(5, 8); obj.someFn.deleteRef(5, 8); a.deep(value, [], "Pre"); obj.someFn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; obj.someFn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; obj.someFn(77, 11); obj.someFn.clear(); a.deep(value, [10, 88], "Clear all"); }; memoizee-0.4.15/test/methods.js000066400000000000000000000014041377604527300164100ustar00rootroot00000000000000/* eslint id-length: 0 */ "use strict"; var d = require("d"); module.exports = function (t, a) { var value = [], obj = {}; Object.defineProperties( obj, t({ someFn: d( function (x, y) { a(this, obj); return x + y; }, { refCounter: true, dispose: function (val) { value.push(val); } } ) }) ); obj = Object.create(obj); obj.someFn(3, 7); obj.someFn(5, 8); obj.someFn(12, 4); a.deep(value, [], "Pre"); obj.someFn(5, 8); obj.someFn.deleteRef(5, 8); a.deep(value, [], "Pre"); obj.someFn.deleteRef(5, 8); a.deep(value, [13], "#1"); value = []; obj.someFn.deleteRef(12, 4); a.deep(value, [16], "#2"); value = []; obj.someFn(77, 11); obj.someFn.clear(); a.deep(value, [10, 88], "Clear all"); }; memoizee-0.4.15/test/normalizers/000077500000000000000000000000001377604527300167555ustar00rootroot00000000000000memoizee-0.4.15/test/normalizers/get-1.js000066400000000000000000000023421377604527300202310ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; var memoize = require("../.."); module.exports = { "": function (t, a) { var i = 0 , fn = function (x) { ++i; return x; }; fn = memoize(fn); return { "No arg": function () { i = 0; a(fn(), undefined, "First"); a(fn(), undefined, "Second"); a(fn(), undefined, "Third"); a(i, 1, "Called once"); }, "Arg": function () { var x = {}; i = 0; a(fn(x, 8), x, "First"); a(fn(x, 4), x, "Second"); a(fn(x, 2), x, "Third"); a(i, 1, "Called once"); }, "Other Arg": function () { var x = {}; i = 0; a(fn(x, 2), x, "First"); a(fn(x, 9), x, "Second"); a(fn(x, 3), x, "Third"); a(i, 1, "Called once"); } }; }, "Delete": function (a) { var i = 0, fn, mfn, x = {}; fn = function (a, b, c) { return a + ++i; }; mfn = memoize(fn, { length: 1 }); a(mfn(3), 4, "Init"); a(mfn(4, x, 1), 6, "Init #2"); mfn.delete(4); a(mfn(3, x, 1), 4, "Cached"); mfn(3, x, 1); a(i, 2, "Pre clear"); mfn.delete(3, x, 1); a(i, 2, "After clear"); a(mfn(3, x, 1), 6, "Reinit"); a(i, 3, "Reinit count"); a(mfn(3, x, 1), 6, "Reinit Cached"); a(i, 3, "Reinit count"); } }; memoizee-0.4.15/test/normalizers/get-fixed.js000066400000000000000000000037661377604527300212030ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; var memoize = require("../.."); module.exports = { "": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return [x, y, z]; } , r; fn = memoize(fn); return { "No args": function () { i = 0; a.deep(r = fn(), [undefined, undefined, undefined], "First"); a(fn(), r, "Second"); a(fn(), r, "Third"); a(i, 1, "Called once"); }, "Some Args": function () { var x = {}; i = 0; a.deep(r = fn(x, 8), [x, 8, undefined], "First"); a(fn(x, 8), r, "Second"); a(fn(x, 8), r, "Third"); a(i, 1, "Called once"); return { Other: function () { a.deep(r = fn(x, 5), [x, 5, undefined], "Second"); a(fn(x, 5), r, "Third"); a(i, 2, "Called once"); } }; }, "Full stuff": function () { var x = {}; i = 0; a.deep(r = fn(x, 8, 23, 98), [x, 8, 23], "First"); a(fn(x, 8, 23, 43), r, "Second"); a(fn(x, 8, 23, 9), r, "Third"); a(i, 1, "Called once"); return { Other: function () { a.deep(r = fn(x, 23, 8, 13), [x, 23, 8], "Second"); a(fn(x, 23, 8, 22), r, "Third"); a(i, 2, "Called once"); } }; } }; }, "Delete": function (a) { var i = 0, fn, mfn, x = {}; fn = function (a, b, c) { return a + ++i; }; mfn = memoize(fn); a(mfn(3, x, 1), 4, "Init"); a(mfn(4, x, 1), 6, "Init #2"); mfn.delete(4, x, 1); a(mfn(3, x, 1), 4, "Cached"); mfn(3, x, 1); a(i, 2, "Pre clear"); mfn.delete(3, x, 1); a(i, 2, "After clear"); a(mfn(3, x, 1), 6, "Reinit"); a(i, 3, "Reinit count"); a(mfn(3, x, 1), 6, "Reinit Cached"); a(i, 3, "Reinit count"); }, "Clear": function (a) { var i = 0, fn, x = {}; fn = function () { ++i; return arguments; }; fn = memoize(fn, { length: 3 }); fn(1, x, 3); fn(1, x, 4); fn(1, x, 3); fn(1, x, 4); a(i, 2, "Pre clear"); fn.clear(); fn(1, x, 3); fn(1, x, 4); fn(1, x, 3); fn(1, x, 4); a(i, 4, "After clear"); } }; memoizee-0.4.15/test/normalizers/get-primitive-fixed.js000066400000000000000000000024171377604527300232010ustar00rootroot00000000000000/* eslint id-length: 0 */ "use strict"; var memoize = require("../.."); module.exports = { "": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return x + y + z; } , mfn , y = { toString: function () { return "foo"; } }; mfn = memoize(fn, { primitive: true }); a(mfn(y, "bar", "zeta"), "foobarzeta", "#1"); a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2"); a(i, 1, "Called once"); }, "Delete": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return x + y + z; } , mfn , y = { toString: function () { return "foo"; } }; mfn = memoize(fn, { primitive: true }); a(mfn(y, "bar", "zeta"), "foobarzeta", "#1"); a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2"); a(i, 1, "Called once"); mfn.delete( "foo", { toString: function () { return "bar"; } }, "zeta" ); a(mfn(y, "bar", "zeta"), "foobarzeta", "#3"); a(i, 2, "Called twice"); }, "Clear": function (a) { var i = 0, fn; fn = memoize(function (x) { if (++i < 2) fn(x); }); a.throws(function () { fn("foo"); }, "CIRCULAR_INVOCATION"); i = 0; fn = memoize(function (x, y) { if (++i < 2) fn(x, y); }); a.throws(function () { fn("foo", "bar"); }, "CIRCULAR_INVOCATION"); } }; memoizee-0.4.15/test/normalizers/get.js000066400000000000000000000027261377604527300201010ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; var aFrom = require("es5-ext/array/from") , memoize = require("../.."); module.exports = function () { return { "": function (a) { var i = 0 , fn = function () { ++i; return arguments; } , r; fn = memoize(fn, { length: false }); return { "No args": function () { i = 0; a.deep(aFrom(r = fn()), [], "First"); a(fn(), r, "Second"); a(fn(), r, "Third"); a(i, 1, "Called once"); }, "Some Args": function () { var x = {}; i = 0; a.deep(aFrom(r = fn(x, 8)), [x, 8], "First"); a(fn(x, 8), r, "Second"); a(fn(x, 8), r, "Third"); a(i, 1, "Called once"); }, "Many args": function () { var x = {}; i = 0; a.deep(aFrom(r = fn(x, 8, 23, 98)), [x, 8, 23, 98], "First"); a(fn(x, 8, 23, 98), r, "Second"); a(fn(x, 8, 23, 98), r, "Third"); a(i, 1, "Called once"); } }; }, "Delete": function (a) { var i = 0, fn, mfn, x = {}; fn = function (a, b, c) { return a + ++i; }; mfn = memoize(fn, { length: false }); a(mfn(3, x, 1), 4, "Init"); a(mfn(4, x, 1), 6, "Init #2"); mfn.delete(4, x, 1); a(mfn(3, x, 1), 4, "Cached"); mfn(3, x, 1); a(i, 2, "Pre clear"); mfn.delete(3, x, 1); a(i, 2, "After clear"); a(mfn(3, x, 1), 6, "Reinit"); a(i, 3, "Reinit count"); a(mfn(3, x, 1), 6, "Reinit Cached"); a(i, 3, "Reinit count"); } }; }; memoizee-0.4.15/test/normalizers/primitive.js000066400000000000000000000011401377604527300213170ustar00rootroot00000000000000/* eslint id-length: 0 */ "use strict"; var memoize = require("../..") , join = Array.prototype.join; module.exports = function (a) { var i = 0 , fn = function () { ++i; return join.call(arguments, "|"); } , y = { toString: function () { return "foo"; } } , mfn; mfn = memoize(fn, { primitive: true, length: false }); a(mfn(y, "bar", "zeta"), "foo|bar|zeta", "#1"); a(mfn("foo", "bar", "zeta"), "foo|bar|zeta", "#2"); a(i, 1, "Called once"); a(mfn(y, "bar"), "foo|bar", "#3"); a(i, 2, "Called twice"); a(mfn(y, "bar"), "foo|bar", "#4"); a(i, 2, "Called twice #2"); }; memoizee-0.4.15/test/plain.js000066400000000000000000000020521377604527300160500ustar00rootroot00000000000000/* eslint id-length: 0 */ "use strict"; module.exports = function (t) { return { "": function (a) { var i = 0 , fn = function (x) { ++i; return x; } , mfn , y = { toString: function () { return "foo"; } }; mfn = t(fn, { primitive: true }); a(typeof mfn, "function", "Returns"); a(mfn.__memoized__, true, "Marked"); a(t(mfn), mfn, "Do not memoize memoized"); a(mfn(y), y, "#1"); a(mfn("foo"), y, "#2"); a(i, 1, "Called once"); }, "Clear cache": function (a) { var i = 0 , fn = function (x, y, z) { ++i; return x + y + z; } , mfn , y = { toString: function () { return "foo"; } }; mfn = t(fn, { primitive: true }); a(mfn(y, "bar", "zeta"), "foobarzeta", "#1"); a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2"); a(i, 1, "Called once"); mfn.delete( "foo", { toString: function () { return "bar"; } }, "zeta" ); a(mfn(y, "bar", "zeta"), "foobarzeta", "#3"); a(i, 2, "Called twice"); } }; }; memoizee-0.4.15/test/profile.js000066400000000000000000000007171377604527300164130ustar00rootroot00000000000000/* eslint no-empty-function: 0 */ "use strict"; var memoize = require("../plain"); module.exports = function (t, a) { memoize(function () {})(); memoize(function () {}, { profileName: "test" })(); a(typeof t.statistics, "object", "Access to statistics"); a(Object.keys(t.statistics).length, 2, "Statistics collected including named function"); a(typeof t.log, "function", "Access to log function"); a(typeof t.log(), "string", "Log outputs string"); }; memoizee-0.4.15/test/weak-plain.js000066400000000000000000000016641377604527300170050ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; require("../ext/dispose"); require("../ext/ref-counter"); module.exports = function (t, a) { var value = [], obj = {}, memoized, count = 0, x, y, z; memoized = t( function (arg, x, y) { a(arg, obj); return x + y; }, { refCounter: true, dispose: function (val) { value.push(val); } } ); a(memoized(obj, 3, 7), 10); a(memoized(obj, 5, 8), 13); a(memoized(obj, 12, 4), 16); a.deep(value, [], "Pre"); a(memoized(obj, 5, 8), 13); memoized.deleteRef(obj, 5, 8); a.deep(value, [], "Pre"); memoized.deleteRef(obj, 5, 8); a.deep(value, [13], "#1"); value = []; memoized.deleteRef(obj, 12, 4); a.deep(value, [16], "#2"); value = []; memoized(obj, 77, 11); x = {}; y = {}; z = {}; memoized = t(function (arg) { return ++count; }); a(memoized(x), 1); a(memoized(y), 2); a(memoized(x), 1); a(memoized(z), 3); a(count, 3); }; memoizee-0.4.15/test/weak.js000066400000000000000000000020631377604527300156760ustar00rootroot00000000000000/* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */ "use strict"; module.exports = function (t, a, d) { var value = [], obj = {}, memoized, count = 0, x, y, z; memoized = t( function (arg, x, y) { a(arg, obj); return x + y; }, { refCounter: true, dispose: function (val) { value.push(val); } } ); a(memoized(obj, 3, 7), 10); a(memoized(obj, 5, 8), 13); a(memoized(obj, 12, 4), 16); a.deep(value, [], "Pre"); a(memoized(obj, 5, 8), 13); memoized.deleteRef(obj, 5, 8); a.deep(value, [], "Pre"); memoized.deleteRef(obj, 5, 8); a.deep(value, [13], "#1"); value = []; memoized.deleteRef(obj, 12, 4); a.deep(value, [16], "#2"); value = []; memoized(obj, 77, 11); x = {}; y = {}; z = {}; memoized = t(function (arg) { return ++count; }); a(memoized(x), 1); a(memoized(y), 2); a(memoized(x), 1); a(memoized(z), 3); a(count, 3); count = 0; memoized = t( function (arg) { return ++count; }, { maxAge: 1 } ); memoized(obj); setTimeout(function () { memoized(obj); a(count, 2); d(); }, 100); }; memoizee-0.4.15/weak-plain.js000066400000000000000000000001131377604527300160120ustar00rootroot00000000000000"use strict"; module.exports = require("./lib/weak")(require("./plain")); memoizee-0.4.15/weak.js000066400000000000000000000001061377604527300147130ustar00rootroot00000000000000"use strict"; module.exports = require("./lib/weak")(require("./"));