hexo-adsense 1.1.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/LICENSE +0 -0
  3. package/dist/index.cjs +12799 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.js +12782 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +12782 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/dist/source/adblock.css +2 -0
  10. package/dist/source/adblock.js +1 -0
  11. package/{lib → dist}/source/article-ads.css +0 -0
  12. package/dist/source/article-ads.js +1 -0
  13. package/dist/source/article-ads.old.js +1 -0
  14. package/{lib → dist}/source/blogger-redirect.html +0 -0
  15. package/dist/source/https.js +1 -0
  16. package/{lib → dist}/source/in_article_ads_test.html +0 -0
  17. package/package.json +122 -38
  18. package/{README.md → readme.md} +27 -8
  19. package/gulpfile.ts +0 -100
  20. package/lib/index.d.ts +0 -1
  21. package/lib/index.js +0 -91
  22. package/lib/packages/hexo-cli-extras/lib/env.d.ts +0 -3
  23. package/lib/packages/hexo-cli-extras/lib/env.js +0 -49
  24. package/lib/packages/hexo-extend-injector2/index.d.ts +0 -2
  25. package/lib/packages/hexo-extend-injector2/index.js +0 -12
  26. package/lib/packages/hexo-extend-injector2/lib/bundle/css-bundle.d.ts +0 -2
  27. package/lib/packages/hexo-extend-injector2/lib/bundle/css-bundle.js +0 -64
  28. package/lib/packages/hexo-extend-injector2/lib/bundle/css-generator.d.ts +0 -2
  29. package/lib/packages/hexo-extend-injector2/lib/bundle/css-generator.js +0 -12
  30. package/lib/packages/hexo-extend-injector2/lib/bundle/handle-data-pre.d.ts +0 -2
  31. package/lib/packages/hexo-extend-injector2/lib/bundle/handle-data-pre.js +0 -28
  32. package/lib/packages/hexo-extend-injector2/lib/bundle/js-bundle.d.ts +0 -2
  33. package/lib/packages/hexo-extend-injector2/lib/bundle/js-bundle.js +0 -32
  34. package/lib/packages/hexo-extend-injector2/lib/bundle/js-generator.d.ts +0 -2
  35. package/lib/packages/hexo-extend-injector2/lib/bundle/js-generator.js +0 -8
  36. package/lib/packages/hexo-extend-injector2/lib/default-config.d.ts +0 -39
  37. package/lib/packages/hexo-extend-injector2/lib/default-config.js +0 -46
  38. package/lib/packages/hexo-extend-injector2/lib/filter.d.ts +0 -2
  39. package/lib/packages/hexo-extend-injector2/lib/filter.js +0 -22
  40. package/lib/packages/hexo-extend-injector2/lib/helper/injector.d.ts +0 -2
  41. package/lib/packages/hexo-extend-injector2/lib/helper/injector.js +0 -5
  42. package/lib/packages/hexo-extend-injector2/lib/helper/next-inject.d.ts +0 -2
  43. package/lib/packages/hexo-extend-injector2/lib/helper/next-inject.js +0 -12
  44. package/lib/packages/hexo-extend-injector2/lib/injector.d.ts +0 -29
  45. package/lib/packages/hexo-extend-injector2/lib/injector.js +0 -125
  46. package/lib/packages/hexo-extend-injector2/lib/load.d.ts +0 -3
  47. package/lib/packages/hexo-extend-injector2/lib/load.js +0 -37
  48. package/lib/packages/hexo-extend-injector2/lib/next-point.d.ts +0 -2
  49. package/lib/packages/hexo-extend-injector2/lib/next-point.js +0 -18
  50. package/lib/packages/hexo-extend-injector2/lib/next.d.ts +0 -2
  51. package/lib/packages/hexo-extend-injector2/lib/next.js +0 -99
  52. package/lib/packages/hexo-extend-injector2/lib/order.d.ts +0 -5
  53. package/lib/packages/hexo-extend-injector2/lib/order.js +0 -8
  54. package/lib/source/adblock.css +0 -2
  55. package/lib/source/adblock.js +0 -1
  56. package/lib/source/article-ads.js +0 -1
  57. package/lib/source/article-ads.old.js +0 -1
  58. package/lib/source/https.js +0 -1
  59. package/lib/src/article-ads.d.ts +0 -20
  60. package/lib/src/article-ads.js +0 -310
  61. package/lib/src/checkLocalHost.d.ts +0 -10
  62. package/lib/src/checkLocalHost.js +0 -37
  63. package/lib/src/config.d.ts +0 -14
  64. package/lib/src/config.js +0 -50
  65. package/lib/src/html-parser.d.ts +0 -8
  66. package/lib/src/html-parser.js +0 -26
  67. package/lib/src/log.d.ts +0 -1
  68. package/lib/src/log.js +0 -11
  69. package/lib/src/logger.d.ts +0 -3
  70. package/lib/src/logger.js +0 -25
  71. package/lib/src/md5.d.ts +0 -7
  72. package/lib/src/md5.js +0 -30
  73. package/lib/src/safestringify.d.ts +0 -2
  74. package/lib/src/safestringify.js +0 -125
  75. package/lib/src/utils.d.ts +0 -31
  76. package/lib/src/utils.js +0 -160
  77. package/lib/src/versionParser.d.ts +0 -27
  78. package/lib/src/versionParser.js +0 -40
  79. package/packages/@types/.gitmodules +0 -8
  80. package/packages/@types/LICENSE +0 -21
  81. package/packages/@types/README.md +0 -27
  82. package/packages/@types/hexo/HexoConfig.d.ts +0 -216
  83. package/packages/@types/hexo/LICENSE +0 -21
  84. package/packages/@types/hexo/README.md +0 -16
  85. package/packages/@types/hexo/index.d.ts +0 -849
  86. package/packages/@types/hexo/package.json +0 -34
  87. package/packages/@types/hexo-bunyan/LICENSE +0 -21
  88. package/packages/@types/hexo-bunyan/README.md +0 -16
  89. package/packages/@types/hexo-bunyan/index.d.ts +0 -9
  90. package/packages/@types/hexo-bunyan/package.json +0 -24
  91. package/packages/@types/hexo-log/LICENSE +0 -21
  92. package/packages/@types/hexo-log/README.md +0 -16
  93. package/packages/@types/hexo-log/index.d.ts +0 -159
  94. package/packages/@types/hexo-log/package.json +0 -28
  95. package/packages/@types/index.js +0 -2
  96. package/packages/@types/package.json +0 -30
  97. package/packages/@types/test/package.json +0 -15
  98. package/packages/@types/test/through2.ts +0 -6
  99. package/packages/@types/tsconfig.json +0 -14
  100. package/packages/@types/typings/index.d.ts +0 -7
  101. package/packages/@types/typings/kill-port.d.ts +0 -3
  102. package/packages/@types/typings/markdown-it-abbr.d.ts +0 -4
  103. package/packages/@types/typings/markdown-it-footnote.d.ts +0 -4
  104. package/packages/@types/typings/markdown-it-image-figures.d.ts +0 -4
  105. package/packages/@types/typings/markdown-it-mark.d.ts +0 -4
  106. package/packages/@types/typings/markdown-it-sub.d.ts +0 -7
  107. package/packages/@types/typings/markdown-it-sup.d.ts +0 -7
  108. package/packages/hexo-cli-extras/.eslintrc +0 -13
  109. package/packages/hexo-cli-extras/LICENSE.md +0 -20
  110. package/packages/hexo-cli-extras/README.md +0 -146
  111. package/packages/hexo-cli-extras/docs/basic.gif +0 -0
  112. package/packages/hexo-cli-extras/docs/gui.gif +0 -0
  113. package/packages/hexo-cli-extras/docs/remove.png +0 -0
  114. package/packages/hexo-cli-extras/docs/rename.png +0 -0
  115. package/packages/hexo-cli-extras/index.js +0 -48
  116. package/packages/hexo-cli-extras/lib/edit.js +0 -209
  117. package/packages/hexo-cli-extras/lib/env.js +0 -52
  118. package/packages/hexo-cli-extras/lib/extensions.js +0 -23
  119. package/packages/hexo-cli-extras/lib/integrate.js +0 -87
  120. package/packages/hexo-cli-extras/lib/isolate.js +0 -179
  121. package/packages/hexo-cli-extras/lib/remove.js +0 -145
  122. package/packages/hexo-cli-extras/lib/rename.js +0 -171
  123. package/packages/hexo-cli-extras/package.json +0 -34
  124. package/packages/hexo-extend-injector2/.eslintrc +0 -4
  125. package/packages/hexo-extend-injector2/.github/workflows/linter.yml +0 -27
  126. package/packages/hexo-extend-injector2/.github/workflows/tester.yml +0 -24
  127. package/packages/hexo-extend-injector2/.vscode/settings.json +0 -10
  128. package/packages/hexo-extend-injector2/LICENSE +0 -165
  129. package/packages/hexo-extend-injector2/README-ZH.md +0 -189
  130. package/packages/hexo-extend-injector2/README.md +0 -191
  131. package/packages/hexo-extend-injector2/USAGE.md +0 -38
  132. package/packages/hexo-extend-injector2/index.js +0 -14
  133. package/packages/hexo-extend-injector2/lib/bundle/css-bundle.js +0 -79
  134. package/packages/hexo-extend-injector2/lib/bundle/css-generator.js +0 -13
  135. package/packages/hexo-extend-injector2/lib/bundle/handle-data-pre.js +0 -29
  136. package/packages/hexo-extend-injector2/lib/bundle/js-bundle.js +0 -42
  137. package/packages/hexo-extend-injector2/lib/bundle/js-generator.js +0 -10
  138. package/packages/hexo-extend-injector2/lib/default-config.js +0 -52
  139. package/packages/hexo-extend-injector2/lib/filter.js +0 -24
  140. package/packages/hexo-extend-injector2/lib/helper/injector.js +0 -6
  141. package/packages/hexo-extend-injector2/lib/helper/next-inject.js +0 -14
  142. package/packages/hexo-extend-injector2/lib/injector.js +0 -128
  143. package/packages/hexo-extend-injector2/lib/load.js +0 -45
  144. package/packages/hexo-extend-injector2/lib/next-point.js +0 -19
  145. package/packages/hexo-extend-injector2/lib/next.js +0 -87
  146. package/packages/hexo-extend-injector2/lib/order.js +0 -9
  147. package/packages/hexo-extend-injector2/package.json +0 -49
  148. package/packages/hexo-extend-injector2/renovate.json +0 -6
  149. package/packages/hexo-extend-injector2/test/.eslintrc +0 -3
  150. package/packages/hexo-extend-injector2/test/index.js +0 -18
  151. package/packages/hexo-extend-injector2/test/lib/bundle/bundle-css.js +0 -86
  152. package/packages/hexo-extend-injector2/test/lib/bundle/bundle-js.js +0 -36
  153. package/packages/hexo-extend-injector2/test/lib/bundle/test.css +0 -3
  154. package/packages/hexo-extend-injector2/test/lib/bundle/test.js +0 -5
  155. package/packages/hexo-extend-injector2/test/lib/helper/injector.js +0 -45
  156. package/packages/hexo-extend-injector2/test/lib/helper/next-inject.js +0 -69
  157. package/packages/hexo-extend-injector2/test/lib/hexo-compatible.js +0 -250
  158. package/packages/hexo-extend-injector2/test/lib/hexo-injector.js +0 -37
  159. package/packages/hexo-extend-injector2/test/lib/injector.js +0 -93
  160. package/packages/memoize/CHANGELOG.md +0 -82
  161. package/packages/memoize/CHANGES +0 -124
  162. package/packages/memoize/LICENSE +0 -15
  163. package/packages/memoize/README.md +0 -503
  164. package/packages/memoize/ext/async.js +0 -154
  165. package/packages/memoize/ext/dispose.js +0 -33
  166. package/packages/memoize/ext/max-age.js +0 -90
  167. package/packages/memoize/ext/max.js +0 -27
  168. package/packages/memoize/ext/promise.js +0 -147
  169. package/packages/memoize/ext/ref-counter.js +0 -48
  170. package/packages/memoize/index.js +0 -34
  171. package/packages/memoize/lib/configure-map.js +0 -182
  172. package/packages/memoize/lib/methods.js +0 -32
  173. package/packages/memoize/lib/registered-extensions.js +0 -1
  174. package/packages/memoize/lib/resolve-length.js +0 -15
  175. package/packages/memoize/lib/resolve-normalize.js +0 -17
  176. package/packages/memoize/lib/resolve-resolve.js +0 -21
  177. package/packages/memoize/lib/weak.js +0 -134
  178. package/packages/memoize/methods-plain.js +0 -3
  179. package/packages/memoize/methods.js +0 -3
  180. package/packages/memoize/normalizers/get-1.js +0 -29
  181. package/packages/memoize/normalizers/get-fixed.js +0 -71
  182. package/packages/memoize/normalizers/get-primitive-fixed.js +0 -16
  183. package/packages/memoize/normalizers/get.js +0 -90
  184. package/packages/memoize/normalizers/primitive.js +0 -9
  185. package/packages/memoize/package.json +0 -60
  186. package/packages/memoize/plain.js +0 -37
  187. package/packages/memoize/profile.js +0 -107
  188. package/packages/memoize/repository.md +0 -1
  189. package/packages/memoize/weak-plain.js +0 -3
  190. package/packages/memoize/weak.js +0 -3
  191. package/tsconfig.json +0 -39
  192. package/unused.md +0 -11
@@ -1,503 +0,0 @@
1
- [![*nix build status][nix-build-image]][nix-build-url]
2
- [![Windows build status][win-build-image]][win-build-url]
3
- ![Transpilation status][transpilation-image]
4
- [![npm version][npm-image]][npm-url]
5
-
6
- # Memoizee
7
-
8
- ## Complete memoize/cache solution for JavaScript
9
-
10
- _Originally derived from [es5-ext](https://github.com/medikoo/es5-ext) package._
11
-
12
- 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
13
-
14
- ### Features
15
-
16
- * Works with any type of function arguments – **no serialization is needed**
17
- * Works with [**any length of function arguments**](#arguments-length). Length can be set as fixed or dynamic.
18
- * One of the [**fastest**](#benchmarks) available solutions.
19
- * Support for [**promises**](#promise-returning-functions) and [**asynchronous functions**](#nodejs-callback-style-functions)
20
- * [**Primitive mode**](#primitive-mode) which assures fast performance when arguments are convertible to strings.
21
- * [**WeakMap based mode**](#weakmap-based-configurations) for garbage collection friendly configuration
22
- * Can be configured [**for methods**](#memoizing-methods) (when `this` counts in)
23
- * Cache [**can be cleared manually**](#manual-clean-up) or [**after specified timeout**](#expire-cache-after-given-period-of-time)
24
- * Cache size can be **[limited on LRU basis](#limiting-cache-size)**
25
- * Optionally [**accepts resolvers**](#argument-resolvers) that normalize function arguments before passing them to underlying function.
26
- * Optional [**reference counter mode**](#reference-counter), that allows more sophisticated cache management
27
- * [**Profile tool**](#profiling--statistics) that provides valuable usage statistics
28
- * Covered by [**over 500 unit tests**](#tests)
29
-
30
- ### Installation
31
-
32
- In your project path — **note the two `e`'s in `memoizee`:**
33
-
34
- $ npm install memoizee
35
-
36
- _`memoize` name was already taken, therefore project is published as `memoizee` on NPM._
37
-
38
- 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/)
39
-
40
- ### Usage
41
-
42
- ```javascript
43
- var memoize = require("memoizee");
44
-
45
- var fn = function(one, two, three) {
46
- /* ... */
47
- };
48
-
49
- memoized = memoize(fn);
50
-
51
- memoized("foo", 3, "bar");
52
- memoized("foo", 3, "bar"); // Cache hit
53
- ```
54
-
55
- __Note__: Invocations that throw exceptions are not cached.
56
-
57
- ### Configuration
58
-
59
- All below options can be applied in any combination
60
-
61
- #### Arguments length
62
-
63
- By default fixed number of arguments that function take is assumed (it's read from function's `length` property) this can be overridden:
64
-
65
- ```javascript
66
- memoized = memoize(fn, { length: 2 });
67
-
68
- memoized("foo"); // Assumed: 'foo', undefined
69
- memoized("foo", undefined); // Cache hit
70
-
71
- memoized("foo", 3, {}); // Third argument is ignored (but passed to underlying function)
72
- memoized("foo", 3, 13); // Cache hit
73
- ```
74
-
75
- __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
76
-
77
- Dynamic _length_ behavior can be forced by setting _length_ to `false`, that means memoize will work with any number of arguments.
78
-
79
- ```javascript
80
- memoized = memoize(fn, { length: false });
81
-
82
- memoized("foo");
83
- memoized("foo"); // Cache hit
84
- memoized("foo", undefined);
85
- memoized("foo", undefined); // Cache hit
86
-
87
- memoized("foo", 3, {});
88
- memoized("foo", 3, 13);
89
- memoized("foo", 3, 13); // Cache hit
90
- ```
91
-
92
- #### Primitive mode
93
-
94
- 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.**
95
-
96
- ```javascript
97
- memoized = memoize(fn, { primitive: true });
98
-
99
- memoized("/path/one");
100
- memoized("/path/one"); // Cache hit
101
- ```
102
-
103
- #### Cache id resolution (normalization)
104
-
105
- By default cache id for given call is resolved either by:
106
-
107
- * 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.
108
- * 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)
109
-
110
- 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.
111
-
112
- ##### Writing custom cache id normalizers
113
-
114
- There's a `normalizer` option through which we can pass custom cache id normalization function
115
- 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:
116
-
117
- ```javascript
118
- var mfn = memoize(
119
- function(hash) {
120
- // body of memoized function
121
- },
122
- {
123
- normalizer: function(args) {
124
- // args is arguments object as accessible in memoized function
125
- return JSON.stringify(args[0]);
126
- }
127
- }
128
- );
129
-
130
- mfn({ foo: "bar" });
131
- mfn({ foo: "bar" }); // Cache hit
132
- ```
133
-
134
- #### Argument resolvers
135
-
136
- 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:
137
-
138
- ```javascript
139
- memoized = memoize(fn, { length: 2, resolvers: [String, Boolean] });
140
-
141
- memoized(12, [1, 2, 3].length);
142
- memoized("12", true); // Cache hit
143
- memoized(
144
- {
145
- toString: function() {
146
- return "12";
147
- }
148
- },
149
- {}
150
- ); // Cache hit
151
- ```
152
-
153
- **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`.
154
-
155
- Similarly **if you want to memoize functions by their code representation not by their objects, you should use primitive mode**.
156
-
157
- #### Memoizing asynchronous functions
158
-
159
- ##### Promise returning functions
160
-
161
- With _promise_ option we indicate that we memoize a function that returns promise.
162
-
163
- The difference from natural behavior is that in case when promise was rejected with exception,
164
- the result is immediately removed from memoize cache, and not kept as further reusable result.
165
-
166
- ```javascript
167
- var afn = function(a, b) {
168
- return new Promise(function(res) {
169
- res(a + b);
170
- });
171
- };
172
- memoized = memoize(afn, { promise: true });
173
-
174
- memoized(3, 7);
175
- memoized(3, 7); // Cache hit
176
- ```
177
-
178
- ###### Important notice on internal promises handling
179
-
180
- Default handling stands purely on _then_ which has side-effect of muting eventual unhandled rejection notifications.
181
- Alternatively we can other (explained below), by stating with `promise` option desired mode:
182
-
183
- ```javascript
184
- memoized = memoize(afn, { promise: "done:finally" });
185
- ```
186
-
187
- Supported modes
188
-
189
- * `then` _(default)_. Values are resolved purely by passing callbacks to `promise.then`. **Side effect is that eventual unhandled rejection on given promise
190
- come with no logged warning!**, and that to avoid implied error swallowing both states are resolved tick after callbacks were invoked
191
-
192
- * `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!**.
193
-
194
- * `done:finally` The only method that may work with no side-effects assuming that promise implementaion does not throw unconditionally
195
- 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)
196
-
197
- ##### Node.js callback style functions
198
-
199
- With _async_ option we indicate that we memoize asynchronous (Node.js style) function
200
- Operations that result with an error are not cached.
201
-
202
- ```javascript
203
- afn = function(a, b, cb) {
204
- setTimeout(function() {
205
- cb(null, a + b);
206
- }, 200);
207
- };
208
- memoized = memoize(afn, { async: true });
209
-
210
- memoized(3, 7, function(err, res) {
211
- memoized(3, 7, function(err, res) {
212
- // Cache hit
213
- });
214
- });
215
-
216
- memoized(3, 7, function(err, res) {
217
- // Cache hit
218
- });
219
- ```
220
-
221
- #### Memoizing methods
222
-
223
- 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:
224
-
225
- ```javascript
226
- var Foo = function() {
227
- this.bar = memoize(this.bar.bind(this), { someOption: true });
228
- // ... constructor logic
229
- };
230
- Foo.prototype.bar = function() {
231
- // ... method logic
232
- };
233
- ```
234
-
235
- There's a lazy methods descriptor generator provided:
236
-
237
- ```javascript
238
- var d = require("d");
239
- var memoizeMethods = require("memoizee/methods");
240
-
241
- var Foo = function() {
242
- // ... constructor logic
243
- };
244
- Object.defineProperties(
245
- Foo.prototype,
246
- memoizeMethods({
247
- bar: d(
248
- function() {
249
- // ... method logic
250
- },
251
- { someOption: true }
252
- )
253
- })
254
- );
255
- ```
256
-
257
- #### WeakMap based configurations
258
-
259
- 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.
260
-
261
- This mode works only for functions of which first argument is expected to be an object.
262
- It can be combined with other options mentioned across documentation. However due to WeakMap specificity global clear is not possible.
263
-
264
- ```javascript
265
- var memoize = require("memoizee/weak");
266
-
267
- var memoized = memoize(function(obj) {
268
- return Object.keys(obj);
269
- });
270
-
271
- var obj = { foo: true, bar: false };
272
- memoized(obj);
273
- memoized(obj); // Cache hit
274
- ```
275
-
276
- #### Cache handling
277
-
278
- ##### Manual clean up:
279
-
280
- Delete data for particular call.
281
-
282
- ```javascript
283
- memoized.delete("foo", true);
284
- ```
285
-
286
- Arguments passed to `delete` are treated with same rules as input arguments passed to function
287
-
288
- Clear all cached data:
289
-
290
- ```javascript
291
- memoized.clear();
292
- ```
293
-
294
- ##### Expire cache after given period of time
295
-
296
- With _maxAge_ option we can ensure that cache for given call is cleared after predefined period of time (in milliseconds)
297
-
298
- ```javascript
299
- memoized = memoize(fn, { maxAge: 1000 }); // 1 second
300
-
301
- memoized("foo", 3);
302
- memoized("foo", 3); // Cache hit
303
- setTimeout(function() {
304
- memoized("foo", 3); // No longer in cache, re-executed
305
- memoized("foo", 3); // Cache hit
306
- }, 2000);
307
- ```
308
-
309
- 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:
310
-
311
- ```javascript
312
- memoized = memoize(fn, { maxAge: 1000, preFetch: true }); // Defaults to 0.33
313
-
314
- memoized("foo", 3);
315
- memoized("foo", 3); // Cache hit
316
-
317
- setTimeout(function() {
318
- memoized("foo", 3); // Cache hit
319
- }, 500);
320
-
321
- setTimeout(function() {
322
- memoized("foo", 3); // Cache hit, silently pre-fetched in next tick
323
- }, 800);
324
-
325
- setTimeout(function() {
326
- memoized("foo", 3); // Cache hit
327
- }, 1300);
328
- ```
329
-
330
- _Pre-fetch_ timespan can be customized:
331
-
332
- ```javascript
333
- memoized = memoize(fn, { maxAge: 1000, preFetch: 0.6 });
334
-
335
- memoized("foo", 3);
336
- memoized("foo", 3); // Cache hit
337
-
338
- setTimeout(function() {
339
- memoized("foo", 3); // Cache hit, silently pre-fetched in next tick
340
- }, 500);
341
-
342
- setTimeout(function() {
343
- memoized("foo", 3); // Cache hit
344
- }, 1300);
345
- ```
346
-
347
- _Thanks [@puzrin](https://github.com/puzrin) for helpful suggestions concerning this functionality_
348
-
349
- ##### Reference counter
350
-
351
- We can track number of references returned from cache, and manually delete them. When the last reference is cleared, the cache is purged automatically:
352
-
353
- ```javascript
354
- memoized = memoize(fn, { refCounter: true });
355
-
356
- memoized("foo", 3); // refs: 1
357
- memoized("foo", 3); // Cache hit, refs: 2
358
- memoized("foo", 3); // Cache hit, refs: 3
359
- memoized.deleteRef("foo", 3); // refs: 2
360
- memoized.deleteRef("foo", 3); // refs: 1
361
- memoized.deleteRef("foo", 3); // refs: 0, Cache purged for 'foo', 3
362
- memoized("foo", 3); // Re-executed, refs: 1
363
- ```
364
-
365
- ##### Limiting cache size
366
-
367
- 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.
368
-
369
- 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).
370
-
371
- ```javascript
372
- memoized = memoize(fn, { max: 2 });
373
-
374
- memoized("foo", 3);
375
- memoized("bar", 7);
376
- memoized("foo", 3); // Cache hit
377
- memoized("bar", 7); // Cache hit
378
- memoized("lorem", 11); // Cache cleared for 'foo', 3
379
- memoized("bar", 7); // Cache hit
380
- memoized("foo", 3); // Re-executed, Cache cleared for 'lorem', 11
381
- memoized("lorem", 11); // Re-executed, Cache cleared for 'bar', 7
382
- memoized("foo", 3); // Cache hit
383
- memoized("bar", 7); // Re-executed, Cache cleared for 'lorem', 11
384
- ```
385
-
386
- ##### Registering dispose callback
387
-
388
- You can register a callback to be called on each value removed from the cache:
389
-
390
- ```javascript
391
- memoized = memoize(fn, {
392
- dispose: function(value) {
393
- /*…*/
394
- }
395
- });
396
-
397
- var foo3 = memoized("foo", 3);
398
- var bar7 = memoized("bar", 7);
399
- memoized.clear("foo", 3); // Dispose called with foo3 value
400
- memoized.clear("bar", 7); // Dispose called with bar7 value
401
- ```
402
-
403
- ### Benchmarks
404
-
405
- 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:
406
-
407
- $ npm install underscore lodash lru-cache secondary-cache
408
-
409
- Example output taken under Node v0.10.35 on 2011 MBP Pro:
410
-
411
- ```
412
- Fibonacci 3000 x10:
413
-
414
- 1: 15ms Memoizee (primitive mode)
415
- 2: 15ms Underscore
416
- 3: 18ms lru-cache LRU (max: 1000)
417
- 4: 21ms secondary-cache LRU (max: 1000)
418
- 5: 37ms Lo-dash
419
- 6: 62ms Memoizee (primitive mode) LRU (max: 1000)
420
- 7: 163ms Memoizee (object mode) LRU (max: 1000)
421
- 8: 195ms Memoizee (object mode)
422
- ```
423
-
424
- ### Profiling & Statistics
425
-
426
- 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.
427
-
428
- **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**
429
-
430
- ```javascript
431
- var memProfile = require('memoizee/profile');
432
- ...
433
- ...
434
- memoize(fn);
435
- ...
436
- memoize(fn, { profileName: 'Some Function' })
437
- ...
438
- memoize(fn, { profileName: 'Another Function' })
439
- ```
440
-
441
- Access statistics at any time:
442
-
443
- ```javascript
444
- memProfile.statistics; // Statistics accessible for programmatic use
445
- console.log(memProfile.log()); // Output statistics data in readable form
446
- ```
447
-
448
- Example console output:
449
-
450
- ```
451
- ------------------------------------------------------------
452
- Memoize statistics:
453
-
454
- Init Cache %Cache Source location
455
- 11604 35682 75.46 (all)
456
- 2112 19901 90.41 Some Function, at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:276:12
457
- 2108 9087 81.17 Another Function, at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:293:10
458
- 6687 2772 29.31 at /Users/medikoo/Projects/_packages/next/lib/fs/watch.js:125:9
459
- 697 3922 84.91 at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:277:15
460
- ------------------------------------------------------------
461
- ```
462
-
463
- * _Init_ – Initial hits
464
- * _Cache_ – Cache hits
465
- * _%Cache_ – What's the percentage of cache hits (of all function calls)
466
- * _Source location_ – Where in the source code given memoization was initialized
467
-
468
- ### Tests
469
-
470
- $ npm test
471
-
472
- Project cross-browser compatibility to be supported by:
473
-
474
- <a href="https://browserstack.com"><img src="https://bstacksupport.zendesk.com/attachments/token/Pj5uf2x5GU9BvWErqAr51Jh2R/?name=browserstack-logo-600x315.png" height="150" /></a>
475
-
476
- ## Security contact information
477
-
478
- To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
479
-
480
- ---
481
-
482
- <div align="center">
483
- <b>
484
- <a href="https://tidelift.com/subscription/pkg/npm-memoizee?utm_source=npm-memoizee&utm_medium=referral&utm_campaign=readme">Get professional support for d with a Tidelift subscription</a>
485
- </b>
486
- <br>
487
- <sub>
488
- Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
489
- </sub>
490
- </div>
491
-
492
- ### Contributors
493
-
494
- * [@puzrin](https://github.com/puzrin) (Vitaly Puzrin)
495
- * Proposal and help with coining right _pre-fetch_ logic for [_maxAge_](https://github.com/medikoo/memoize#expire-cache-after-given-period-of-time) variant
496
-
497
- [nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/memoizee/branches/master/shields_badge.svg
498
- [nix-build-url]: https://semaphoreci.com/medikoo-org/memoizee
499
- [win-build-image]: https://ci.appveyor.com/api/projects/status/hsxubnbwe89c26bu?svg=true
500
- [win-build-url]: https://ci.appveyor.com/project/medikoo/memoizee
501
- [transpilation-image]: https://img.shields.io/badge/transpilation-free-brightgreen.svg
502
- [npm-image]: https://img.shields.io/npm/v/memoizee.svg
503
- [npm-url]: https://www.npmjs.com/package/memoizee
@@ -1,154 +0,0 @@
1
- /* eslint consistent-this: 0, no-shadow:0, no-eq-null: 0, eqeqeq: 0, no-unused-vars: 0 */
2
-
3
- // Support for asynchronous functions
4
-
5
- "use strict";
6
-
7
- var aFrom = require("es5-ext/array/from")
8
- , objectMap = require("es5-ext/object/map")
9
- , mixin = require("es5-ext/object/mixin")
10
- , defineLength = require("es5-ext/function/_define-length")
11
- , nextTick = require("next-tick");
12
-
13
- var slice = Array.prototype.slice, apply = Function.prototype.apply, create = Object.create;
14
-
15
- require("../lib/registered-extensions").async = function (tbi, conf) {
16
- var waiting = create(null)
17
- , cache = create(null)
18
- , base = conf.memoized
19
- , original = conf.original
20
- , currentCallback
21
- , currentContext
22
- , currentArgs;
23
-
24
- // Initial
25
- conf.memoized = defineLength(function (arg) {
26
- var args = arguments, last = args[args.length - 1];
27
- if (typeof last === "function") {
28
- currentCallback = last;
29
- args = slice.call(args, 0, -1);
30
- }
31
- return base.apply(currentContext = this, currentArgs = args);
32
- }, base);
33
- try { mixin(conf.memoized, base); }
34
- catch (ignore) {}
35
-
36
- // From cache (sync)
37
- conf.on("get", function (id) {
38
- var cb, context, args;
39
- if (!currentCallback) return;
40
-
41
- // Unresolved
42
- if (waiting[id]) {
43
- if (typeof waiting[id] === "function") waiting[id] = [waiting[id], currentCallback];
44
- else waiting[id].push(currentCallback);
45
- currentCallback = null;
46
- return;
47
- }
48
-
49
- // Resolved, assure next tick invocation
50
- cb = currentCallback;
51
- context = currentContext;
52
- args = currentArgs;
53
- currentCallback = currentContext = currentArgs = null;
54
- nextTick(function () {
55
- var data;
56
- if (hasOwnProperty.call(cache, id)) {
57
- data = cache[id];
58
- conf.emit("getasync", id, args, context);
59
- apply.call(cb, data.context, data.args);
60
- } else {
61
- // Purged in a meantime, we shouldn't rely on cached value, recall
62
- currentCallback = cb;
63
- currentContext = context;
64
- currentArgs = args;
65
- base.apply(context, args);
66
- }
67
- });
68
- });
69
-
70
- // Not from cache
71
- conf.original = function () {
72
- var args, cb, origCb, result;
73
- if (!currentCallback) return apply.call(original, this, arguments);
74
- args = aFrom(arguments);
75
- cb = function self(err) {
76
- var cb, args, id = self.id;
77
- if (id == null) {
78
- // Shouldn't happen, means async callback was called sync way
79
- nextTick(apply.bind(self, this, arguments));
80
- return undefined;
81
- }
82
- delete self.id;
83
- cb = waiting[id];
84
- delete waiting[id];
85
- if (!cb) {
86
- // Already processed,
87
- // outcome of race condition: asyncFn(1, cb), asyncFn.clear(), asyncFn(1, cb)
88
- return undefined;
89
- }
90
- args = aFrom(arguments);
91
- if (conf.has(id)) {
92
- if (err) {
93
- conf.delete(id);
94
- } else {
95
- cache[id] = { context: this, args: args };
96
- conf.emit("setasync", id, typeof cb === "function" ? 1 : cb.length);
97
- }
98
- }
99
- if (typeof cb === "function") {
100
- result = apply.call(cb, this, args);
101
- } else {
102
- cb.forEach(function (cb) { result = apply.call(cb, this, args); }, this);
103
- }
104
- return result;
105
- };
106
- origCb = currentCallback;
107
- currentCallback = currentContext = currentArgs = null;
108
- args.push(cb);
109
- result = apply.call(original, this, args);
110
- cb.cb = origCb;
111
- currentCallback = cb;
112
- return result;
113
- };
114
-
115
- // After not from cache call
116
- conf.on("set", function (id) {
117
- if (!currentCallback) {
118
- conf.delete(id);
119
- return;
120
- }
121
- if (waiting[id]) {
122
- // Race condition: asyncFn(1, cb), asyncFn.clear(), asyncFn(1, cb)
123
- if (typeof waiting[id] === "function") waiting[id] = [waiting[id], currentCallback.cb];
124
- else waiting[id].push(currentCallback.cb);
125
- } else {
126
- waiting[id] = currentCallback.cb;
127
- }
128
- delete currentCallback.cb;
129
- currentCallback.id = id;
130
- currentCallback = null;
131
- });
132
-
133
- // On delete
134
- conf.on("delete", function (id) {
135
- var result;
136
- // If false, we don't have value yet, so we assume that intention is not
137
- // to memoize this call. After value is obtained we don't cache it but
138
- // gracefully pass to callback
139
- if (hasOwnProperty.call(waiting, id)) return;
140
- if (!cache[id]) return;
141
- result = cache[id];
142
- delete cache[id];
143
- conf.emit("deleteasync", id, slice.call(result.args, 1));
144
- });
145
-
146
- // On clear
147
- conf.on("clear", function () {
148
- var oldCache = cache;
149
- cache = create(null);
150
- conf.emit(
151
- "clearasync", objectMap(oldCache, function (data) { return slice.call(data.args, 1); })
152
- );
153
- });
154
- };
@@ -1,33 +0,0 @@
1
- // Call dispose callback on each cache purge
2
-
3
- "use strict";
4
-
5
- var callable = require("es5-ext/object/valid-callable")
6
- , forEach = require("es5-ext/object/for-each")
7
- , extensions = require("../lib/registered-extensions")
8
-
9
- , apply = Function.prototype.apply;
10
-
11
- extensions.dispose = function (dispose, conf, options) {
12
- var del;
13
- callable(dispose);
14
- if ((options.async && extensions.async) || (options.promise && extensions.promise)) {
15
- conf.on("deleteasync", del = function (id, resultArray) {
16
- apply.call(dispose, null, resultArray);
17
- });
18
- conf.on("clearasync", function (cache) {
19
- forEach(cache, function (result, id) {
20
- del(id, result);
21
- });
22
- });
23
- return;
24
- }
25
- conf.on("delete", del = function (id, result) {
26
- dispose(result);
27
- });
28
- conf.on("clear", function (cache) {
29
- forEach(cache, function (result, id) {
30
- del(id, result);
31
- });
32
- });
33
- };