WIP - add extractor, generate snippet_data
This commit is contained in:
55
node_modules/mem/index.js
generated
vendored
Normal file
55
node_modules/mem/index.js
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
const mimicFn = require('mimic-fn');
|
||||
|
||||
const cacheStore = new WeakMap();
|
||||
|
||||
const defaultCacheKey = function (x) {
|
||||
if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) {
|
||||
return x;
|
||||
}
|
||||
|
||||
return JSON.stringify(arguments);
|
||||
};
|
||||
|
||||
module.exports = (fn, opts) => {
|
||||
opts = Object.assign({
|
||||
cacheKey: defaultCacheKey,
|
||||
cache: new Map()
|
||||
}, opts);
|
||||
|
||||
const memoized = function () {
|
||||
const cache = cacheStore.get(memoized);
|
||||
const key = opts.cacheKey.apply(null, arguments);
|
||||
|
||||
if (cache.has(key)) {
|
||||
const c = cache.get(key);
|
||||
|
||||
if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) {
|
||||
return c.data;
|
||||
}
|
||||
}
|
||||
|
||||
const ret = fn.apply(null, arguments);
|
||||
|
||||
cache.set(key, {
|
||||
data: ret,
|
||||
maxAge: Date.now() + (opts.maxAge || 0)
|
||||
});
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
mimicFn(memoized, fn);
|
||||
|
||||
cacheStore.set(memoized, opts.cache);
|
||||
|
||||
return memoized;
|
||||
};
|
||||
|
||||
module.exports.clear = fn => {
|
||||
const cache = cacheStore.get(fn);
|
||||
|
||||
if (cache && typeof cache.clear === 'function') {
|
||||
cache.clear();
|
||||
}
|
||||
};
|
||||
21
node_modules/mem/license
generated
vendored
Normal file
21
node_modules/mem/license
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
9
node_modules/mem/node_modules/mimic-fn/index.js
generated
vendored
Normal file
9
node_modules/mem/node_modules/mimic-fn/index.js
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
module.exports = (to, from) => {
|
||||
// TODO: use `Reflect.ownKeys()` when targeting Node.js 6
|
||||
for (const prop of Object.getOwnPropertyNames(from).concat(Object.getOwnPropertySymbols(from))) {
|
||||
Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop));
|
||||
}
|
||||
|
||||
return to;
|
||||
};
|
||||
9
node_modules/mem/node_modules/mimic-fn/license
generated
vendored
Normal file
9
node_modules/mem/node_modules/mimic-fn/license
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
72
node_modules/mem/node_modules/mimic-fn/package.json
generated
vendored
Normal file
72
node_modules/mem/node_modules/mimic-fn/package.json
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
{
|
||||
"_from": "mimic-fn@^1.0.0",
|
||||
"_id": "mimic-fn@1.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-jf84uxzwiuiIVKiOLpfYk7N46TSy8ubTonmneY9vrpHNAnp0QBt2BxWV9dO3/j+BoVAb+a5G6YDPW3M5HOdMWQ==",
|
||||
"_location": "/mem/mimic-fn",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "mimic-fn@^1.0.0",
|
||||
"name": "mimic-fn",
|
||||
"escapedName": "mimic-fn",
|
||||
"rawSpec": "^1.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/mem"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-1.2.0.tgz",
|
||||
"_shasum": "820c86a39334640e99516928bd03fca88057d022",
|
||||
"_spec": "mimic-fn@^1.0.0",
|
||||
"_where": "/Users/stefanfejes/Projects/30-seconds-of-python-code/node_modules/mem",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/mimic-fn/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "Make a function mimic another one",
|
||||
"devDependencies": {
|
||||
"ava": "*",
|
||||
"xo": "*"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/sindresorhus/mimic-fn#readme",
|
||||
"keywords": [
|
||||
"function",
|
||||
"mimic",
|
||||
"imitate",
|
||||
"rename",
|
||||
"copy",
|
||||
"inherit",
|
||||
"properties",
|
||||
"name",
|
||||
"func",
|
||||
"fn",
|
||||
"set",
|
||||
"infer",
|
||||
"change"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "mimic-fn",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sindresorhus/mimic-fn.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava"
|
||||
},
|
||||
"version": "1.2.0"
|
||||
}
|
||||
68
node_modules/mem/node_modules/mimic-fn/readme.md
generated
vendored
Normal file
68
node_modules/mem/node_modules/mimic-fn/readme.md
generated
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
# mimic-fn [](https://travis-ci.org/sindresorhus/mimic-fn)
|
||||
|
||||
> Make a function mimic another one
|
||||
|
||||
Useful when you wrap a function in another function and like to preserve the original name and other properties.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install mimic-fn
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const mimicFn = require('mimic-fn');
|
||||
|
||||
function foo() {}
|
||||
foo.unicorn = '🦄';
|
||||
|
||||
function wrapper() {
|
||||
return foo() {};
|
||||
}
|
||||
|
||||
console.log(wrapper.name);
|
||||
//=> 'wrapper'
|
||||
|
||||
mimicFn(wrapper, foo);
|
||||
|
||||
console.log(wrapper.name);
|
||||
//=> 'foo'
|
||||
|
||||
console.log(wrapper.unicorn);
|
||||
//=> '🦄'
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
|
||||
|
||||
### mimicFn(to, from)
|
||||
|
||||
It will modify `to` and return it.
|
||||
|
||||
#### to
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Mimicking function.
|
||||
|
||||
#### from
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Function to mimic.
|
||||
|
||||
|
||||
## Related
|
||||
|
||||
- [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
77
node_modules/mem/package.json
generated
vendored
Normal file
77
node_modules/mem/package.json
generated
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
{
|
||||
"_from": "mem@^1.1.0",
|
||||
"_id": "mem@1.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-Xt1StIXKHZAP5kiVUFOZoN+kX3Y=",
|
||||
"_location": "/mem",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "mem@^1.1.0",
|
||||
"name": "mem",
|
||||
"escapedName": "mem",
|
||||
"rawSpec": "^1.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/os-locale"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz",
|
||||
"_shasum": "5edd52b485ca1d900fe64895505399a0dfa45f76",
|
||||
"_spec": "mem@^1.1.0",
|
||||
"_where": "/Users/stefanfejes/Projects/30-seconds-of-python-code/node_modules/os-locale",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/mem/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"mimic-fn": "^1.0.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",
|
||||
"devDependencies": {
|
||||
"ava": "*",
|
||||
"delay": "^1.1.0",
|
||||
"xo": "*"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/sindresorhus/mem#readme",
|
||||
"keywords": [
|
||||
"memoize",
|
||||
"function",
|
||||
"mem",
|
||||
"memoization",
|
||||
"cache",
|
||||
"caching",
|
||||
"optimize",
|
||||
"performance",
|
||||
"ttl",
|
||||
"expire",
|
||||
"promise"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "mem",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sindresorhus/mem.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava"
|
||||
},
|
||||
"version": "1.1.0",
|
||||
"xo": {
|
||||
"esnext": true
|
||||
}
|
||||
}
|
||||
147
node_modules/mem/readme.md
generated
vendored
Normal file
147
node_modules/mem/readme.md
generated
vendored
Normal file
@ -0,0 +1,147 @@
|
||||
# mem [](https://travis-ci.org/sindresorhus/mem)
|
||||
|
||||
> [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install --save mem
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
|
||||
let i = 0;
|
||||
const counter = () => ++i;
|
||||
const memoized = mem(counter);
|
||||
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
// cached as it's the same arguments
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
// not cached anymore as the arguments changed
|
||||
memoized('bar');
|
||||
//=> 2
|
||||
|
||||
memoized('bar');
|
||||
//=> 2
|
||||
```
|
||||
|
||||
##### Works fine with promise returning functions
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
|
||||
let i = 0;
|
||||
const counter = () => Promise.resolve(++i);
|
||||
const memoized = mem(counter);
|
||||
|
||||
memoized().then(a => {
|
||||
console.log(a);
|
||||
//=> 1
|
||||
|
||||
memoized().then(b => {
|
||||
// the return value didn't increase as it's cached
|
||||
console.log(b);
|
||||
//=> 1
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
const got = require('got');
|
||||
const memGot = mem(got, {maxAge: 1000});
|
||||
|
||||
memGot('sindresorhus.com').then(() => {
|
||||
// this call is cached
|
||||
memGot('sindresorhus.com').then(() => {
|
||||
setTimeout(() => {
|
||||
// this call is not cached as the cache has expired
|
||||
memGot('sindresorhus.com').then(() => {});
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### mem(fn, [options])
|
||||
|
||||
#### fn
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Function to be memoized.
|
||||
|
||||
#### options
|
||||
|
||||
##### maxAge
|
||||
|
||||
Type: `number`<br>
|
||||
Default: `Infinity`
|
||||
|
||||
Milliseconds until the cache expires.
|
||||
|
||||
##### cacheKey
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
|
||||
|
||||
You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
|
||||
|
||||
##### cache
|
||||
|
||||
Type: `Object`<br>
|
||||
Default: `new Map()`
|
||||
|
||||
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, and optionally `.clear()`. You could for example use a `WeakMap` instead.
|
||||
|
||||
### mem.clear(fn)
|
||||
|
||||
Clear all cached data of a memoized function.
|
||||
|
||||
#### fn
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Memoized function.
|
||||
|
||||
|
||||
## Tips
|
||||
|
||||
### Cache statistics
|
||||
|
||||
If you want to know how many times your cache had a hit or a miss, you can make use of [stats-map](https://github.com/SamVerschueren/stats-map) as a replacement for the default cache.
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
const StatsMap = require('stats-map');
|
||||
const got = require('got');
|
||||
|
||||
const cache = new StatsMap();
|
||||
const memGot = mem(got, {cache});
|
||||
|
||||
memGot('sindresorhus.com')
|
||||
.then(() => memGot('sindresorhus.com'))
|
||||
.then(() => memGot('sindresorhus.com'));
|
||||
|
||||
console.log(cache.stats);
|
||||
//=> {hits: 2, misses: 1}
|
||||
```
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
Reference in New Issue
Block a user