mirror of https://github.com/lukechilds/node.git
Browse Source
PR-URL: https://github.com/nodejs/node/pull/14417 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Refael Ackermann <refack@gmail.com>v6
Rich Trott
8 years ago
124 changed files with 8670 additions and 1841 deletions
File diff suppressed because it is too large
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,43 @@ |
|||||
|
{ |
||||
|
"env": { |
||||
|
"browser": true, |
||||
|
"node": true, |
||||
|
"commonjs": true, |
||||
|
"es6": true |
||||
|
}, |
||||
|
"extends": "eslint:recommended", |
||||
|
"rules": { |
||||
|
"strict": 2, |
||||
|
"indent": 0, |
||||
|
"linebreak-style": 0, |
||||
|
"quotes": 0, |
||||
|
"semi": 0, |
||||
|
"no-cond-assign": 1, |
||||
|
"no-constant-condition": 1, |
||||
|
"no-duplicate-case": 1, |
||||
|
"no-empty": 1, |
||||
|
"no-ex-assign": 1, |
||||
|
"no-extra-boolean-cast": 1, |
||||
|
"no-extra-semi": 1, |
||||
|
"no-fallthrough": 1, |
||||
|
"no-func-assign": 1, |
||||
|
"no-global-assign": 1, |
||||
|
"no-implicit-globals": 2, |
||||
|
"no-inner-declarations": ["error", "functions"], |
||||
|
"no-irregular-whitespace": 2, |
||||
|
"no-loop-func": 1, |
||||
|
"no-multi-str": 1, |
||||
|
"no-mixed-spaces-and-tabs": 1, |
||||
|
"no-proto": 1, |
||||
|
"no-sequences": 1, |
||||
|
"no-throw-literal": 1, |
||||
|
"no-unmodified-loop-condition": 1, |
||||
|
"no-useless-call": 1, |
||||
|
"no-void": 1, |
||||
|
"no-with": 2, |
||||
|
"wrap-iife": 1, |
||||
|
"no-redeclare": 1, |
||||
|
"no-unused-vars": ["error", { "vars": "all", "args": "none" }], |
||||
|
"no-sparse-arrays": 1 |
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
Copyright (c) 2014 IndigoUnited |
||||
|
|
||||
|
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. |
@ -0,0 +1,85 @@ |
|||||
|
# cross-spawn |
||||
|
|
||||
|
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Build status][appveyor-image]][appveyor-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] |
||||
|
|
||||
|
[npm-url]:https://npmjs.org/package/cross-spawn |
||||
|
[downloads-image]:http://img.shields.io/npm/dm/cross-spawn.svg |
||||
|
[npm-image]:http://img.shields.io/npm/v/cross-spawn.svg |
||||
|
[travis-url]:https://travis-ci.org/IndigoUnited/node-cross-spawn |
||||
|
[travis-image]:http://img.shields.io/travis/IndigoUnited/node-cross-spawn/master.svg |
||||
|
[appveyor-url]:https://ci.appveyor.com/project/satazor/node-cross-spawn |
||||
|
[appveyor-image]:https://img.shields.io/appveyor/ci/satazor/node-cross-spawn/master.svg |
||||
|
[david-dm-url]:https://david-dm.org/IndigoUnited/node-cross-spawn |
||||
|
[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-cross-spawn.svg |
||||
|
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-cross-spawn#info=devDependencies |
||||
|
[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-cross-spawn.svg |
||||
|
|
||||
|
A cross platform solution to node's spawn and spawnSync. |
||||
|
|
||||
|
|
||||
|
## Installation |
||||
|
|
||||
|
`$ npm install cross-spawn` |
||||
|
|
||||
|
If you are using `spawnSync` on node 0.10 or older, you will also need to install `spawn-sync`: |
||||
|
|
||||
|
`$ npm install spawn-sync` |
||||
|
|
||||
|
|
||||
|
## Why |
||||
|
|
||||
|
Node has issues when using spawn on Windows: |
||||
|
|
||||
|
- It ignores [PATHEXT](https://github.com/joyent/node/issues/2318) |
||||
|
- It does not support [shebangs](http://pt.wikipedia.org/wiki/Shebang) |
||||
|
- No `options.shell` support on node < v6 |
||||
|
- It does not allow you to run `del` or `dir` |
||||
|
|
||||
|
All these issues are handled correctly by `cross-spawn`. |
||||
|
There are some known modules, such as [win-spawn](https://github.com/ForbesLindesay/win-spawn), that try to solve this but they are either broken or provide faulty escaping of shell arguments. |
||||
|
|
||||
|
|
||||
|
## Usage |
||||
|
|
||||
|
Exactly the same way as node's [`spawn`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) or [`spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options), so it's a drop in replacement. |
||||
|
|
||||
|
|
||||
|
```js |
||||
|
var spawn = require('cross-spawn'); |
||||
|
|
||||
|
// Spawn NPM asynchronously |
||||
|
var child = spawn('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' }); |
||||
|
|
||||
|
// Spawn NPM synchronously |
||||
|
var results = spawn.sync('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' }); |
||||
|
``` |
||||
|
|
||||
|
|
||||
|
## Caveats |
||||
|
|
||||
|
#### `options.shell` as an alternative to `cross-spawn` |
||||
|
|
||||
|
Starting from node v6, `spawn` has a `shell` option that allows you run commands from within a shell. This new option solves most of the problems that `cross-spawn` attempts to solve, but: |
||||
|
|
||||
|
- It's not supported in node < v6 |
||||
|
- It has no support for shebangs on Windows |
||||
|
- You must manually escape the command and arguments which is very error prone, specially when passing user input |
||||
|
|
||||
|
If you are using the `shell` option to spawn a command in a cross platform way, consider using `cross-spawn` instead. You have been warned. |
||||
|
|
||||
|
|
||||
|
#### Shebangs |
||||
|
|
||||
|
While `cross-spawn` handles shebangs on Windows, its support is limited: e.g.: it doesn't handle arguments after the path, e.g.: `#!/bin/bash -e`. |
||||
|
|
||||
|
Remember to always test your code on Windows! |
||||
|
|
||||
|
|
||||
|
## Tests |
||||
|
|
||||
|
`$ npm test` |
||||
|
|
||||
|
|
||||
|
## License |
||||
|
|
||||
|
Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php). |
@ -0,0 +1,59 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var cp = require('child_process'); |
||||
|
var parse = require('./lib/parse'); |
||||
|
var enoent = require('./lib/enoent'); |
||||
|
|
||||
|
var cpSpawnSync = cp.spawnSync; |
||||
|
|
||||
|
function spawn(command, args, options) { |
||||
|
var parsed; |
||||
|
var spawned; |
||||
|
|
||||
|
// Parse the arguments
|
||||
|
parsed = parse(command, args, options); |
||||
|
|
||||
|
// Spawn the child process
|
||||
|
spawned = cp.spawn(parsed.command, parsed.args, parsed.options); |
||||
|
|
||||
|
// Hook into child process "exit" event to emit an error if the command
|
||||
|
// does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
|
||||
|
enoent.hookChildProcess(spawned, parsed); |
||||
|
|
||||
|
return spawned; |
||||
|
} |
||||
|
|
||||
|
function spawnSync(command, args, options) { |
||||
|
var parsed; |
||||
|
var result; |
||||
|
|
||||
|
if (!cpSpawnSync) { |
||||
|
try { |
||||
|
cpSpawnSync = require('spawn-sync'); // eslint-disable-line global-require
|
||||
|
} catch (ex) { |
||||
|
throw new Error( |
||||
|
'In order to use spawnSync on node 0.10 or older, you must ' + |
||||
|
'install spawn-sync:\n\n' + |
||||
|
' npm install spawn-sync --save' |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Parse the arguments
|
||||
|
parsed = parse(command, args, options); |
||||
|
|
||||
|
// Spawn the child process
|
||||
|
result = cpSpawnSync(parsed.command, parsed.args, parsed.options); |
||||
|
|
||||
|
// Analyze if the command does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
|
||||
|
result.error = result.error || enoent.verifyENOENTSync(result.status, parsed); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
module.exports = spawn; |
||||
|
module.exports.spawn = spawn; |
||||
|
module.exports.sync = spawnSync; |
||||
|
|
||||
|
module.exports._parse = parse; |
||||
|
module.exports._enoent = enoent; |
@ -0,0 +1,73 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var isWin = process.platform === 'win32'; |
||||
|
var resolveCommand = require('./util/resolveCommand'); |
||||
|
|
||||
|
var isNode10 = process.version.indexOf('v0.10.') === 0; |
||||
|
|
||||
|
function notFoundError(command, syscall) { |
||||
|
var err; |
||||
|
|
||||
|
err = new Error(syscall + ' ' + command + ' ENOENT'); |
||||
|
err.code = err.errno = 'ENOENT'; |
||||
|
err.syscall = syscall + ' ' + command; |
||||
|
|
||||
|
return err; |
||||
|
} |
||||
|
|
||||
|
function hookChildProcess(cp, parsed) { |
||||
|
var originalEmit; |
||||
|
|
||||
|
if (!isWin) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
originalEmit = cp.emit; |
||||
|
cp.emit = function (name, arg1) { |
||||
|
var err; |
||||
|
|
||||
|
// If emitting "exit" event and exit code is 1, we need to check if
|
||||
|
// the command exists and emit an "error" instead
|
||||
|
// See: https://github.com/IndigoUnited/node-cross-spawn/issues/16
|
||||
|
if (name === 'exit') { |
||||
|
err = verifyENOENT(arg1, parsed, 'spawn'); |
||||
|
|
||||
|
if (err) { |
||||
|
return originalEmit.call(cp, 'error', err); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return originalEmit.apply(cp, arguments); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
function verifyENOENT(status, parsed) { |
||||
|
if (isWin && status === 1 && !parsed.file) { |
||||
|
return notFoundError(parsed.original, 'spawn'); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
function verifyENOENTSync(status, parsed) { |
||||
|
if (isWin && status === 1 && !parsed.file) { |
||||
|
return notFoundError(parsed.original, 'spawnSync'); |
||||
|
} |
||||
|
|
||||
|
// If we are in node 10, then we are using spawn-sync; if it exited
|
||||
|
// with -1 it probably means that the command does not exist
|
||||
|
if (isNode10 && status === -1) { |
||||
|
parsed.file = isWin ? parsed.file : resolveCommand(parsed.original); |
||||
|
|
||||
|
if (!parsed.file) { |
||||
|
return notFoundError(parsed.original, 'spawnSync'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
module.exports.hookChildProcess = hookChildProcess; |
||||
|
module.exports.verifyENOENT = verifyENOENT; |
||||
|
module.exports.verifyENOENTSync = verifyENOENTSync; |
||||
|
module.exports.notFoundError = notFoundError; |
@ -0,0 +1,113 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var resolveCommand = require('./util/resolveCommand'); |
||||
|
var hasEmptyArgumentBug = require('./util/hasEmptyArgumentBug'); |
||||
|
var escapeArgument = require('./util/escapeArgument'); |
||||
|
var escapeCommand = require('./util/escapeCommand'); |
||||
|
var readShebang = require('./util/readShebang'); |
||||
|
|
||||
|
var isWin = process.platform === 'win32'; |
||||
|
var skipShellRegExp = /\.(?:com|exe)$/i; |
||||
|
|
||||
|
// Supported in Node >= 6 and >= 4.8
|
||||
|
var supportsShellOption = parseInt(process.version.substr(1).split('.')[0], 10) >= 6 || |
||||
|
parseInt(process.version.substr(1).split('.')[0], 10) === 4 && parseInt(process.version.substr(1).split('.')[1], 10) >= 8; |
||||
|
|
||||
|
function parseNonShell(parsed) { |
||||
|
var shebang; |
||||
|
var needsShell; |
||||
|
var applyQuotes; |
||||
|
|
||||
|
if (!isWin) { |
||||
|
return parsed; |
||||
|
} |
||||
|
|
||||
|
// Detect & add support for shebangs
|
||||
|
parsed.file = resolveCommand(parsed.command); |
||||
|
parsed.file = parsed.file || resolveCommand(parsed.command, true); |
||||
|
shebang = parsed.file && readShebang(parsed.file); |
||||
|
|
||||
|
if (shebang) { |
||||
|
parsed.args.unshift(parsed.file); |
||||
|
parsed.command = shebang; |
||||
|
needsShell = hasEmptyArgumentBug || !skipShellRegExp.test(resolveCommand(shebang) || resolveCommand(shebang, true)); |
||||
|
} else { |
||||
|
needsShell = hasEmptyArgumentBug || !skipShellRegExp.test(parsed.file); |
||||
|
} |
||||
|
|
||||
|
// If a shell is required, use cmd.exe and take care of escaping everything correctly
|
||||
|
if (needsShell) { |
||||
|
// Escape command & arguments
|
||||
|
applyQuotes = (parsed.command !== 'echo'); // Do not quote arguments for the special "echo" command
|
||||
|
parsed.command = escapeCommand(parsed.command); |
||||
|
parsed.args = parsed.args.map(function (arg) { |
||||
|
return escapeArgument(arg, applyQuotes); |
||||
|
}); |
||||
|
|
||||
|
// Make use of cmd.exe
|
||||
|
parsed.args = ['/d', '/s', '/c', '"' + parsed.command + (parsed.args.length ? ' ' + parsed.args.join(' ') : '') + '"']; |
||||
|
parsed.command = process.env.comspec || 'cmd.exe'; |
||||
|
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
||||
|
} |
||||
|
|
||||
|
return parsed; |
||||
|
} |
||||
|
|
||||
|
function parseShell(parsed) { |
||||
|
var shellCommand; |
||||
|
|
||||
|
// If node supports the shell option, there's no need to mimic its behavior
|
||||
|
if (supportsShellOption) { |
||||
|
return parsed; |
||||
|
} |
||||
|
|
||||
|
// Mimic node shell option, see: https://github.com/nodejs/node/blob/b9f6a2dc059a1062776133f3d4fd848c4da7d150/lib/child_process.js#L335
|
||||
|
shellCommand = [parsed.command].concat(parsed.args).join(' '); |
||||
|
|
||||
|
if (isWin) { |
||||
|
parsed.command = typeof parsed.options.shell === 'string' ? parsed.options.shell : process.env.comspec || 'cmd.exe'; |
||||
|
parsed.args = ['/d', '/s', '/c', '"' + shellCommand + '"']; |
||||
|
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
||||
|
} else { |
||||
|
if (typeof parsed.options.shell === 'string') { |
||||
|
parsed.command = parsed.options.shell; |
||||
|
} else if (process.platform === 'android') { |
||||
|
parsed.command = '/system/bin/sh'; |
||||
|
} else { |
||||
|
parsed.command = '/bin/sh'; |
||||
|
} |
||||
|
|
||||
|
parsed.args = ['-c', shellCommand]; |
||||
|
} |
||||
|
|
||||
|
return parsed; |
||||
|
} |
||||
|
|
||||
|
// ------------------------------------------------
|
||||
|
|
||||
|
function parse(command, args, options) { |
||||
|
var parsed; |
||||
|
|
||||
|
// Normalize arguments, similar to nodejs
|
||||
|
if (args && !Array.isArray(args)) { |
||||
|
options = args; |
||||
|
args = null; |
||||
|
} |
||||
|
|
||||
|
args = args ? args.slice(0) : []; // Clone array to avoid changing the original
|
||||
|
options = options || {}; |
||||
|
|
||||
|
// Build our parsed object
|
||||
|
parsed = { |
||||
|
command: command, |
||||
|
args: args, |
||||
|
options: options, |
||||
|
file: undefined, |
||||
|
original: command, |
||||
|
}; |
||||
|
|
||||
|
// Delegate further parsing to shell or non-shell
|
||||
|
return options.shell ? parseShell(parsed) : parseNonShell(parsed); |
||||
|
} |
||||
|
|
||||
|
module.exports = parse; |
@ -0,0 +1,30 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
function escapeArgument(arg, quote) { |
||||
|
// Convert to string
|
||||
|
arg = '' + arg; |
||||
|
|
||||
|
// If we are not going to quote the argument,
|
||||
|
// escape shell metacharacters, including double and single quotes:
|
||||
|
if (!quote) { |
||||
|
arg = arg.replace(/([()%!^<>&|;,"'\s])/g, '^$1'); |
||||
|
} else { |
||||
|
// Sequence of backslashes followed by a double quote:
|
||||
|
// double up all the backslashes and escape the double quote
|
||||
|
arg = arg.replace(/(\\*)"/g, '$1$1\\"'); |
||||
|
|
||||
|
// Sequence of backslashes followed by the end of the string
|
||||
|
// (which will become a double quote later):
|
||||
|
// double up all the backslashes
|
||||
|
arg = arg.replace(/(\\*)$/, '$1$1'); |
||||
|
|
||||
|
// All other backslashes occur literally
|
||||
|
|
||||
|
// Quote the whole thing:
|
||||
|
arg = '"' + arg + '"'; |
||||
|
} |
||||
|
|
||||
|
return arg; |
||||
|
} |
||||
|
|
||||
|
module.exports = escapeArgument; |
@ -0,0 +1,12 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var escapeArgument = require('./escapeArgument'); |
||||
|
|
||||
|
function escapeCommand(command) { |
||||
|
// Do not escape if this command is not dangerous..
|
||||
|
// We do this so that commands like "echo" or "ifconfig" work
|
||||
|
// Quoting them, will make them unaccessible
|
||||
|
return /^[a-z0-9_-]+$/i.test(command) ? command : escapeArgument(command, true); |
||||
|
} |
||||
|
|
||||
|
module.exports = escapeCommand; |
@ -0,0 +1,18 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
// See: https://github.com/IndigoUnited/node-cross-spawn/pull/34#issuecomment-221623455
|
||||
|
function hasEmptyArgumentBug() { |
||||
|
var nodeVer; |
||||
|
|
||||
|
if (process.platform !== 'win32') { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
nodeVer = process.version.substr(1).split('.').map(function (num) { |
||||
|
return parseInt(num, 10); |
||||
|
}); |
||||
|
|
||||
|
return (nodeVer[0] === 0 && nodeVer[1] < 12); |
||||
|
} |
||||
|
|
||||
|
module.exports = hasEmptyArgumentBug(); |
@ -0,0 +1,37 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var fs = require('fs'); |
||||
|
var LRU = require('lru-cache'); |
||||
|
var shebangCommand = require('shebang-command'); |
||||
|
|
||||
|
var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec
|
||||
|
|
||||
|
function readShebang(command) { |
||||
|
var buffer; |
||||
|
var fd; |
||||
|
var shebang; |
||||
|
|
||||
|
// Check if it is in the cache first
|
||||
|
if (shebangCache.has(command)) { |
||||
|
return shebangCache.get(command); |
||||
|
} |
||||
|
|
||||
|
// Read the first 150 bytes from the file
|
||||
|
buffer = new Buffer(150); |
||||
|
|
||||
|
try { |
||||
|
fd = fs.openSync(command, 'r'); |
||||
|
fs.readSync(fd, buffer, 0, 150, 0); |
||||
|
fs.closeSync(fd); |
||||
|
} catch (e) { /* empty */ } |
||||
|
|
||||
|
// Attempt to extract shebang (null is returned if not a shebang)
|
||||
|
shebang = shebangCommand(buffer.toString()); |
||||
|
|
||||
|
// Store the shebang in the cache
|
||||
|
shebangCache.set(command, shebang); |
||||
|
|
||||
|
return shebang; |
||||
|
} |
||||
|
|
||||
|
module.exports = readShebang; |
@ -0,0 +1,31 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var path = require('path'); |
||||
|
var which = require('which'); |
||||
|
var LRU = require('lru-cache'); |
||||
|
|
||||
|
var commandCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec
|
||||
|
|
||||
|
function resolveCommand(command, noExtension) { |
||||
|
var resolved; |
||||
|
|
||||
|
noExtension = !!noExtension; |
||||
|
resolved = commandCache.get(command + '!' + noExtension); |
||||
|
|
||||
|
// Check if its resolved in the cache
|
||||
|
if (commandCache.has(command)) { |
||||
|
return commandCache.get(command); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
resolved = !noExtension ? |
||||
|
which.sync(command) : |
||||
|
which.sync(command, { pathExt: path.delimiter + (process.env.PATHEXT || '') }); |
||||
|
} catch (e) { /* empty */ } |
||||
|
|
||||
|
commandCache.set(command + '!' + noExtension, resolved); |
||||
|
|
||||
|
return resolved; |
||||
|
} |
||||
|
|
||||
|
module.exports = resolveCommand; |
@ -0,0 +1,83 @@ |
|||||
|
{ |
||||
|
"_from": "cross-spawn@^5.1.0", |
||||
|
"_id": "cross-spawn@5.1.0", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk=", |
||||
|
"_location": "/eslint/cross-spawn", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "cross-spawn@^5.1.0", |
||||
|
"name": "cross-spawn", |
||||
|
"escapedName": "cross-spawn", |
||||
|
"rawSpec": "^5.1.0", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^5.1.0" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz", |
||||
|
"_shasum": "e8bd0efee58fcff6f8f94510a0a554bbfa235449", |
||||
|
"_spec": "cross-spawn@^5.1.0", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint", |
||||
|
"author": { |
||||
|
"name": "IndigoUnited", |
||||
|
"email": "hello@indigounited.com", |
||||
|
"url": "http://indigounited.com" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/IndigoUnited/node-cross-spawn/issues/" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"dependencies": { |
||||
|
"lru-cache": "^4.0.1", |
||||
|
"shebang-command": "^1.2.0", |
||||
|
"which": "^1.2.9" |
||||
|
}, |
||||
|
"deprecated": false, |
||||
|
"description": "Cross platform child_process#spawn and child_process#spawnSync", |
||||
|
"devDependencies": { |
||||
|
"@satazor/eslint-config": "^3.0.0", |
||||
|
"eslint": "^3.0.0", |
||||
|
"expect.js": "^0.3.0", |
||||
|
"glob": "^7.0.0", |
||||
|
"mkdirp": "^0.5.1", |
||||
|
"mocha": "^3.0.2", |
||||
|
"once": "^1.4.0", |
||||
|
"rimraf": "^2.5.0" |
||||
|
}, |
||||
|
"files": [ |
||||
|
"index.js", |
||||
|
"lib" |
||||
|
], |
||||
|
"homepage": "https://github.com/IndigoUnited/node-cross-spawn#readme", |
||||
|
"keywords": [ |
||||
|
"spawn", |
||||
|
"spawnSync", |
||||
|
"windows", |
||||
|
"cross", |
||||
|
"platform", |
||||
|
"path", |
||||
|
"ext", |
||||
|
"path-ext", |
||||
|
"path_ext", |
||||
|
"shebang", |
||||
|
"hashbang", |
||||
|
"cmd", |
||||
|
"execute" |
||||
|
], |
||||
|
"license": "MIT", |
||||
|
"main": "index.js", |
||||
|
"name": "cross-spawn", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git://github.com/IndigoUnited/node-cross-spawn.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"lint": "eslint '{*.js,lib/**/*.js,test/**/*.js}'", |
||||
|
"test": "node test/prepare && mocha --bail test/test" |
||||
|
}, |
||||
|
"version": "5.1.0" |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
|
||||
|
The MIT License (MIT) |
||||
|
|
||||
|
Copyright (c) 2013 Mikola Lysenko |
||||
|
|
||||
|
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. |
@ -0,0 +1,237 @@ |
|||||
|
functional-red-black-tree |
||||
|
========================= |
||||
|
A [fully persistent](http://en.wikipedia.org/wiki/Persistent_data_structure) [red-black tree](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree) written 100% in JavaScript. Works both in node.js and in the browser via [browserify](http://browserify.org/). |
||||
|
|
||||
|
Functional (or fully presistent) data structures allow for non-destructive updates. So if you insert an element into the tree, it returns a new tree with the inserted element rather than destructively updating the existing tree in place. Doing this requires using extra memory, and if one were naive it could cost as much as reallocating the entire tree. Instead, this data structure saves some memory by recycling references to previously allocated subtrees. This requires using only O(log(n)) additional memory per update instead of a full O(n) copy. |
||||
|
|
||||
|
Some advantages of this is that it is possible to apply insertions and removals to the tree while still iterating over previous versions of the tree. Functional and persistent data structures can also be useful in many geometric algorithms like point location within triangulations or ray queries, and can be used to analyze the history of executing various algorithms. This added power though comes at a cost, since it is generally a bit slower to use a functional data structure than an imperative version. However, if your application needs this behavior then you may consider using this module. |
||||
|
|
||||
|
# Install |
||||
|
|
||||
|
npm install functional-red-black-tree |
||||
|
|
||||
|
# Example |
||||
|
|
||||
|
Here is an example of some basic usage: |
||||
|
|
||||
|
```javascript |
||||
|
//Load the library |
||||
|
var createTree = require("functional-red-black-tree") |
||||
|
|
||||
|
//Create a tree |
||||
|
var t1 = createTree() |
||||
|
|
||||
|
//Insert some items into the tree |
||||
|
var t2 = t1.insert(1, "foo") |
||||
|
var t3 = t2.insert(2, "bar") |
||||
|
|
||||
|
//Remove something |
||||
|
var t4 = t3.remove(1) |
||||
|
``` |
||||
|
|
||||
|
|
||||
|
# API |
||||
|
|
||||
|
```javascript |
||||
|
var createTree = require("functional-red-black-tree") |
||||
|
``` |
||||
|
|
||||
|
## Overview |
||||
|
|
||||
|
- [Tree methods](#tree-methods) |
||||
|
- [`var tree = createTree([compare])`](#var-tree-=-createtreecompare) |
||||
|
- [`tree.keys`](#treekeys) |
||||
|
- [`tree.values`](#treevalues) |
||||
|
- [`tree.length`](#treelength) |
||||
|
- [`tree.get(key)`](#treegetkey) |
||||
|
- [`tree.insert(key, value)`](#treeinsertkey-value) |
||||
|
- [`tree.remove(key)`](#treeremovekey) |
||||
|
- [`tree.find(key)`](#treefindkey) |
||||
|
- [`tree.ge(key)`](#treegekey) |
||||
|
- [`tree.gt(key)`](#treegtkey) |
||||
|
- [`tree.lt(key)`](#treeltkey) |
||||
|
- [`tree.le(key)`](#treelekey) |
||||
|
- [`tree.at(position)`](#treeatposition) |
||||
|
- [`tree.begin`](#treebegin) |
||||
|
- [`tree.end`](#treeend) |
||||
|
- [`tree.forEach(visitor(key,value)[, lo[, hi]])`](#treeforEachvisitorkeyvalue-lo-hi) |
||||
|
- [`tree.root`](#treeroot) |
||||
|
- [Node properties](#node-properties) |
||||
|
- [`node.key`](#nodekey) |
||||
|
- [`node.value`](#nodevalue) |
||||
|
- [`node.left`](#nodeleft) |
||||
|
- [`node.right`](#noderight) |
||||
|
- [Iterator methods](#iterator-methods) |
||||
|
- [`iter.key`](#iterkey) |
||||
|
- [`iter.value`](#itervalue) |
||||
|
- [`iter.node`](#iternode) |
||||
|
- [`iter.tree`](#itertree) |
||||
|
- [`iter.index`](#iterindex) |
||||
|
- [`iter.valid`](#itervalid) |
||||
|
- [`iter.clone()`](#iterclone) |
||||
|
- [`iter.remove()`](#iterremove) |
||||
|
- [`iter.update(value)`](#iterupdatevalue) |
||||
|
- [`iter.next()`](#iternext) |
||||
|
- [`iter.prev()`](#iterprev) |
||||
|
- [`iter.hasNext`](#iterhasnext) |
||||
|
- [`iter.hasPrev`](#iterhasprev) |
||||
|
|
||||
|
## Tree methods |
||||
|
|
||||
|
### `var tree = createTree([compare])` |
||||
|
Creates an empty functional tree |
||||
|
|
||||
|
* `compare` is an optional comparison function, same semantics as array.sort() |
||||
|
|
||||
|
**Returns** An empty tree ordered by `compare` |
||||
|
|
||||
|
### `tree.keys` |
||||
|
A sorted array of all the keys in the tree |
||||
|
|
||||
|
### `tree.values` |
||||
|
An array array of all the values in the tree |
||||
|
|
||||
|
### `tree.length` |
||||
|
The number of items in the tree |
||||
|
|
||||
|
### `tree.get(key)` |
||||
|
Retrieves the value associated to the given key |
||||
|
|
||||
|
* `key` is the key of the item to look up |
||||
|
|
||||
|
**Returns** The value of the first node associated to `key` |
||||
|
|
||||
|
### `tree.insert(key, value)` |
||||
|
Creates a new tree with the new pair inserted. |
||||
|
|
||||
|
* `key` is the key of the item to insert |
||||
|
* `value` is the value of the item to insert |
||||
|
|
||||
|
**Returns** A new tree with `key` and `value` inserted |
||||
|
|
||||
|
### `tree.remove(key)` |
||||
|
Removes the first item with `key` in the tree |
||||
|
|
||||
|
* `key` is the key of the item to remove |
||||
|
|
||||
|
**Returns** A new tree with the given item removed if it exists |
||||
|
|
||||
|
### `tree.find(key)` |
||||
|
Returns an iterator pointing to the first item in the tree with `key`, otherwise `null`. |
||||
|
|
||||
|
### `tree.ge(key)` |
||||
|
Find the first item in the tree whose key is `>= key` |
||||
|
|
||||
|
* `key` is the key to search for |
||||
|
|
||||
|
**Returns** An iterator at the given element. |
||||
|
|
||||
|
### `tree.gt(key)` |
||||
|
Finds the first item in the tree whose key is `> key` |
||||
|
|
||||
|
* `key` is the key to search for |
||||
|
|
||||
|
**Returns** An iterator at the given element |
||||
|
|
||||
|
### `tree.lt(key)` |
||||
|
Finds the last item in the tree whose key is `< key` |
||||
|
|
||||
|
* `key` is the key to search for |
||||
|
|
||||
|
**Returns** An iterator at the given element |
||||
|
|
||||
|
### `tree.le(key)` |
||||
|
Finds the last item in the tree whose key is `<= key` |
||||
|
|
||||
|
* `key` is the key to search for |
||||
|
|
||||
|
**Returns** An iterator at the given element |
||||
|
|
||||
|
### `tree.at(position)` |
||||
|
Finds an iterator starting at the given element |
||||
|
|
||||
|
* `position` is the index at which the iterator gets created |
||||
|
|
||||
|
**Returns** An iterator starting at position |
||||
|
|
||||
|
### `tree.begin` |
||||
|
An iterator pointing to the first element in the tree |
||||
|
|
||||
|
### `tree.end` |
||||
|
An iterator pointing to the last element in the tree |
||||
|
|
||||
|
### `tree.forEach(visitor(key,value)[, lo[, hi]])` |
||||
|
Walks a visitor function over the nodes of the tree in order. |
||||
|
|
||||
|
* `visitor(key,value)` is a callback that gets executed on each node. If a truthy value is returned from the visitor, then iteration is stopped. |
||||
|
* `lo` is an optional start of the range to visit (inclusive) |
||||
|
* `hi` is an optional end of the range to visit (non-inclusive) |
||||
|
|
||||
|
**Returns** The last value returned by the callback |
||||
|
|
||||
|
### `tree.root` |
||||
|
Returns the root node of the tree |
||||
|
|
||||
|
|
||||
|
## Node properties |
||||
|
Each node of the tree has the following properties: |
||||
|
|
||||
|
### `node.key` |
||||
|
The key associated to the node |
||||
|
|
||||
|
### `node.value` |
||||
|
The value associated to the node |
||||
|
|
||||
|
### `node.left` |
||||
|
The left subtree of the node |
||||
|
|
||||
|
### `node.right` |
||||
|
The right subtree of the node |
||||
|
|
||||
|
## Iterator methods |
||||
|
|
||||
|
### `iter.key` |
||||
|
The key of the item referenced by the iterator |
||||
|
|
||||
|
### `iter.value` |
||||
|
The value of the item referenced by the iterator |
||||
|
|
||||
|
### `iter.node` |
||||
|
The value of the node at the iterator's current position. `null` is iterator is node valid. |
||||
|
|
||||
|
### `iter.tree` |
||||
|
The tree associated to the iterator |
||||
|
|
||||
|
### `iter.index` |
||||
|
Returns the position of this iterator in the sequence. |
||||
|
|
||||
|
### `iter.valid` |
||||
|
Checks if the iterator is valid |
||||
|
|
||||
|
### `iter.clone()` |
||||
|
Makes a copy of the iterator |
||||
|
|
||||
|
### `iter.remove()` |
||||
|
Removes the item at the position of the iterator |
||||
|
|
||||
|
**Returns** A new binary search tree with `iter`'s item removed |
||||
|
|
||||
|
### `iter.update(value)` |
||||
|
Updates the value of the node in the tree at this iterator |
||||
|
|
||||
|
**Returns** A new binary search tree with the corresponding node updated |
||||
|
|
||||
|
### `iter.next()` |
||||
|
Advances the iterator to the next position |
||||
|
|
||||
|
### `iter.prev()` |
||||
|
Moves the iterator backward one element |
||||
|
|
||||
|
### `iter.hasNext` |
||||
|
If true, then the iterator is not at the end of the sequence |
||||
|
|
||||
|
### `iter.hasPrev` |
||||
|
If true, then the iterator is not at the beginning of the sequence |
||||
|
|
||||
|
# Credits |
||||
|
(c) 2013 Mikola Lysenko. MIT License |
@ -0,0 +1,11 @@ |
|||||
|
"use strict" |
||||
|
|
||||
|
var createTree = require("../rbtree.js") |
||||
|
|
||||
|
var t = createTree() |
||||
|
|
||||
|
var s = Date.now() |
||||
|
for(var i=0; i<100000; ++i) { |
||||
|
t = t.insert(Math.random(), Math.random()) |
||||
|
} |
||||
|
console.log(Date.now() - s) |
@ -0,0 +1,68 @@ |
|||||
|
{ |
||||
|
"_from": "functional-red-black-tree@^1.0.1", |
||||
|
"_id": "functional-red-black-tree@1.0.1", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", |
||||
|
"_location": "/eslint/functional-red-black-tree", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "functional-red-black-tree@^1.0.1", |
||||
|
"name": "functional-red-black-tree", |
||||
|
"escapedName": "functional-red-black-tree", |
||||
|
"rawSpec": "^1.0.1", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^1.0.1" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz", |
||||
|
"_shasum": "1b0ab3bd553b2a0d6399d29c0e3ea0b252078327", |
||||
|
"_spec": "functional-red-black-tree@^1.0.1", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint", |
||||
|
"author": { |
||||
|
"name": "Mikola Lysenko" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/mikolalysenko/functional-red-black-tree/issues" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"dependencies": {}, |
||||
|
"deprecated": false, |
||||
|
"description": "A fully persistent balanced binary search tree", |
||||
|
"devDependencies": { |
||||
|
"iota-array": "^0.0.1", |
||||
|
"tape": "^2.12.0" |
||||
|
}, |
||||
|
"directories": { |
||||
|
"test": "test" |
||||
|
}, |
||||
|
"homepage": "https://github.com/mikolalysenko/functional-red-black-tree#readme", |
||||
|
"keywords": [ |
||||
|
"functional", |
||||
|
"red", |
||||
|
"black", |
||||
|
"tree", |
||||
|
"binary", |
||||
|
"search", |
||||
|
"balance", |
||||
|
"persistent", |
||||
|
"fully", |
||||
|
"dynamic", |
||||
|
"data", |
||||
|
"structure" |
||||
|
], |
||||
|
"license": "MIT", |
||||
|
"main": "rbtree.js", |
||||
|
"name": "functional-red-black-tree", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git://github.com/mikolalysenko/functional-red-black-tree.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"test": "tape test/*.js" |
||||
|
}, |
||||
|
"version": "1.0.1" |
||||
|
} |
@ -0,0 +1,996 @@ |
|||||
|
"use strict" |
||||
|
|
||||
|
module.exports = createRBTree |
||||
|
|
||||
|
var RED = 0 |
||||
|
var BLACK = 1 |
||||
|
|
||||
|
function RBNode(color, key, value, left, right, count) { |
||||
|
this._color = color |
||||
|
this.key = key |
||||
|
this.value = value |
||||
|
this.left = left |
||||
|
this.right = right |
||||
|
this._count = count |
||||
|
} |
||||
|
|
||||
|
function cloneNode(node) { |
||||
|
return new RBNode(node._color, node.key, node.value, node.left, node.right, node._count) |
||||
|
} |
||||
|
|
||||
|
function repaint(color, node) { |
||||
|
return new RBNode(color, node.key, node.value, node.left, node.right, node._count) |
||||
|
} |
||||
|
|
||||
|
function recount(node) { |
||||
|
node._count = 1 + (node.left ? node.left._count : 0) + (node.right ? node.right._count : 0) |
||||
|
} |
||||
|
|
||||
|
function RedBlackTree(compare, root) { |
||||
|
this._compare = compare |
||||
|
this.root = root |
||||
|
} |
||||
|
|
||||
|
var proto = RedBlackTree.prototype |
||||
|
|
||||
|
Object.defineProperty(proto, "keys", { |
||||
|
get: function() { |
||||
|
var result = [] |
||||
|
this.forEach(function(k,v) { |
||||
|
result.push(k) |
||||
|
}) |
||||
|
return result |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
Object.defineProperty(proto, "values", { |
||||
|
get: function() { |
||||
|
var result = [] |
||||
|
this.forEach(function(k,v) { |
||||
|
result.push(v) |
||||
|
}) |
||||
|
return result |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Returns the number of nodes in the tree
|
||||
|
Object.defineProperty(proto, "length", { |
||||
|
get: function() { |
||||
|
if(this.root) { |
||||
|
return this.root._count |
||||
|
} |
||||
|
return 0 |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Insert a new item into the tree
|
||||
|
proto.insert = function(key, value) { |
||||
|
var cmp = this._compare |
||||
|
//Find point to insert new node at
|
||||
|
var n = this.root |
||||
|
var n_stack = [] |
||||
|
var d_stack = [] |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
n_stack.push(n) |
||||
|
d_stack.push(d) |
||||
|
if(d <= 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
//Rebuild path to leaf node
|
||||
|
n_stack.push(new RBNode(RED, key, value, null, null, 1)) |
||||
|
for(var s=n_stack.length-2; s>=0; --s) { |
||||
|
var n = n_stack[s] |
||||
|
if(d_stack[s] <= 0) { |
||||
|
n_stack[s] = new RBNode(n._color, n.key, n.value, n_stack[s+1], n.right, n._count+1) |
||||
|
} else { |
||||
|
n_stack[s] = new RBNode(n._color, n.key, n.value, n.left, n_stack[s+1], n._count+1) |
||||
|
} |
||||
|
} |
||||
|
//Rebalance tree using rotations
|
||||
|
//console.log("start insert", key, d_stack)
|
||||
|
for(var s=n_stack.length-1; s>1; --s) { |
||||
|
var p = n_stack[s-1] |
||||
|
var n = n_stack[s] |
||||
|
if(p._color === BLACK || n._color === BLACK) { |
||||
|
break |
||||
|
} |
||||
|
var pp = n_stack[s-2] |
||||
|
if(pp.left === p) { |
||||
|
if(p.left === n) { |
||||
|
var y = pp.right |
||||
|
if(y && y._color === RED) { |
||||
|
//console.log("LLr")
|
||||
|
p._color = BLACK |
||||
|
pp.right = repaint(BLACK, y) |
||||
|
pp._color = RED |
||||
|
s -= 1 |
||||
|
} else { |
||||
|
//console.log("LLb")
|
||||
|
pp._color = RED |
||||
|
pp.left = p.right |
||||
|
p._color = BLACK |
||||
|
p.right = pp |
||||
|
n_stack[s-2] = p |
||||
|
n_stack[s-1] = n |
||||
|
recount(pp) |
||||
|
recount(p) |
||||
|
if(s >= 3) { |
||||
|
var ppp = n_stack[s-3] |
||||
|
if(ppp.left === pp) { |
||||
|
ppp.left = p |
||||
|
} else { |
||||
|
ppp.right = p |
||||
|
} |
||||
|
} |
||||
|
break |
||||
|
} |
||||
|
} else { |
||||
|
var y = pp.right |
||||
|
if(y && y._color === RED) { |
||||
|
//console.log("LRr")
|
||||
|
p._color = BLACK |
||||
|
pp.right = repaint(BLACK, y) |
||||
|
pp._color = RED |
||||
|
s -= 1 |
||||
|
} else { |
||||
|
//console.log("LRb")
|
||||
|
p.right = n.left |
||||
|
pp._color = RED |
||||
|
pp.left = n.right |
||||
|
n._color = BLACK |
||||
|
n.left = p |
||||
|
n.right = pp |
||||
|
n_stack[s-2] = n |
||||
|
n_stack[s-1] = p |
||||
|
recount(pp) |
||||
|
recount(p) |
||||
|
recount(n) |
||||
|
if(s >= 3) { |
||||
|
var ppp = n_stack[s-3] |
||||
|
if(ppp.left === pp) { |
||||
|
ppp.left = n |
||||
|
} else { |
||||
|
ppp.right = n |
||||
|
} |
||||
|
} |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
if(p.right === n) { |
||||
|
var y = pp.left |
||||
|
if(y && y._color === RED) { |
||||
|
//console.log("RRr", y.key)
|
||||
|
p._color = BLACK |
||||
|
pp.left = repaint(BLACK, y) |
||||
|
pp._color = RED |
||||
|
s -= 1 |
||||
|
} else { |
||||
|
//console.log("RRb")
|
||||
|
pp._color = RED |
||||
|
pp.right = p.left |
||||
|
p._color = BLACK |
||||
|
p.left = pp |
||||
|
n_stack[s-2] = p |
||||
|
n_stack[s-1] = n |
||||
|
recount(pp) |
||||
|
recount(p) |
||||
|
if(s >= 3) { |
||||
|
var ppp = n_stack[s-3] |
||||
|
if(ppp.right === pp) { |
||||
|
ppp.right = p |
||||
|
} else { |
||||
|
ppp.left = p |
||||
|
} |
||||
|
} |
||||
|
break |
||||
|
} |
||||
|
} else { |
||||
|
var y = pp.left |
||||
|
if(y && y._color === RED) { |
||||
|
//console.log("RLr")
|
||||
|
p._color = BLACK |
||||
|
pp.left = repaint(BLACK, y) |
||||
|
pp._color = RED |
||||
|
s -= 1 |
||||
|
} else { |
||||
|
//console.log("RLb")
|
||||
|
p.left = n.right |
||||
|
pp._color = RED |
||||
|
pp.right = n.left |
||||
|
n._color = BLACK |
||||
|
n.right = p |
||||
|
n.left = pp |
||||
|
n_stack[s-2] = n |
||||
|
n_stack[s-1] = p |
||||
|
recount(pp) |
||||
|
recount(p) |
||||
|
recount(n) |
||||
|
if(s >= 3) { |
||||
|
var ppp = n_stack[s-3] |
||||
|
if(ppp.right === pp) { |
||||
|
ppp.right = n |
||||
|
} else { |
||||
|
ppp.left = n |
||||
|
} |
||||
|
} |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
//Return new tree
|
||||
|
n_stack[0]._color = BLACK |
||||
|
return new RedBlackTree(cmp, n_stack[0]) |
||||
|
} |
||||
|
|
||||
|
|
||||
|
//Visit all nodes inorder
|
||||
|
function doVisitFull(visit, node) { |
||||
|
if(node.left) { |
||||
|
var v = doVisitFull(visit, node.left) |
||||
|
if(v) { return v } |
||||
|
} |
||||
|
var v = visit(node.key, node.value) |
||||
|
if(v) { return v } |
||||
|
if(node.right) { |
||||
|
return doVisitFull(visit, node.right) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Visit half nodes in order
|
||||
|
function doVisitHalf(lo, compare, visit, node) { |
||||
|
var l = compare(lo, node.key) |
||||
|
if(l <= 0) { |
||||
|
if(node.left) { |
||||
|
var v = doVisitHalf(lo, compare, visit, node.left) |
||||
|
if(v) { return v } |
||||
|
} |
||||
|
var v = visit(node.key, node.value) |
||||
|
if(v) { return v } |
||||
|
} |
||||
|
if(node.right) { |
||||
|
return doVisitHalf(lo, compare, visit, node.right) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Visit all nodes within a range
|
||||
|
function doVisit(lo, hi, compare, visit, node) { |
||||
|
var l = compare(lo, node.key) |
||||
|
var h = compare(hi, node.key) |
||||
|
var v |
||||
|
if(l <= 0) { |
||||
|
if(node.left) { |
||||
|
v = doVisit(lo, hi, compare, visit, node.left) |
||||
|
if(v) { return v } |
||||
|
} |
||||
|
if(h > 0) { |
||||
|
v = visit(node.key, node.value) |
||||
|
if(v) { return v } |
||||
|
} |
||||
|
} |
||||
|
if(h > 0 && node.right) { |
||||
|
return doVisit(lo, hi, compare, visit, node.right) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
proto.forEach = function rbTreeForEach(visit, lo, hi) { |
||||
|
if(!this.root) { |
||||
|
return |
||||
|
} |
||||
|
switch(arguments.length) { |
||||
|
case 1: |
||||
|
return doVisitFull(visit, this.root) |
||||
|
break |
||||
|
|
||||
|
case 2: |
||||
|
return doVisitHalf(lo, this._compare, visit, this.root) |
||||
|
break |
||||
|
|
||||
|
case 3: |
||||
|
if(this._compare(lo, hi) >= 0) { |
||||
|
return |
||||
|
} |
||||
|
return doVisit(lo, hi, this._compare, visit, this.root) |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//First item in list
|
||||
|
Object.defineProperty(proto, "begin", { |
||||
|
get: function() { |
||||
|
var stack = [] |
||||
|
var n = this.root |
||||
|
while(n) { |
||||
|
stack.push(n) |
||||
|
n = n.left |
||||
|
} |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Last item in list
|
||||
|
Object.defineProperty(proto, "end", { |
||||
|
get: function() { |
||||
|
var stack = [] |
||||
|
var n = this.root |
||||
|
while(n) { |
||||
|
stack.push(n) |
||||
|
n = n.right |
||||
|
} |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Find the ith item in the tree
|
||||
|
proto.at = function(idx) { |
||||
|
if(idx < 0) { |
||||
|
return new RedBlackTreeIterator(this, []) |
||||
|
} |
||||
|
var n = this.root |
||||
|
var stack = [] |
||||
|
while(true) { |
||||
|
stack.push(n) |
||||
|
if(n.left) { |
||||
|
if(idx < n.left._count) { |
||||
|
n = n.left |
||||
|
continue |
||||
|
} |
||||
|
idx -= n.left._count |
||||
|
} |
||||
|
if(!idx) { |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
idx -= 1 |
||||
|
if(n.right) { |
||||
|
if(idx >= n.right._count) { |
||||
|
break |
||||
|
} |
||||
|
n = n.right |
||||
|
} else { |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
return new RedBlackTreeIterator(this, []) |
||||
|
} |
||||
|
|
||||
|
proto.ge = function(key) { |
||||
|
var cmp = this._compare |
||||
|
var n = this.root |
||||
|
var stack = [] |
||||
|
var last_ptr = 0 |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
stack.push(n) |
||||
|
if(d <= 0) { |
||||
|
last_ptr = stack.length |
||||
|
} |
||||
|
if(d <= 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
stack.length = last_ptr |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
|
||||
|
proto.gt = function(key) { |
||||
|
var cmp = this._compare |
||||
|
var n = this.root |
||||
|
var stack = [] |
||||
|
var last_ptr = 0 |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
stack.push(n) |
||||
|
if(d < 0) { |
||||
|
last_ptr = stack.length |
||||
|
} |
||||
|
if(d < 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
stack.length = last_ptr |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
|
||||
|
proto.lt = function(key) { |
||||
|
var cmp = this._compare |
||||
|
var n = this.root |
||||
|
var stack = [] |
||||
|
var last_ptr = 0 |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
stack.push(n) |
||||
|
if(d > 0) { |
||||
|
last_ptr = stack.length |
||||
|
} |
||||
|
if(d <= 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
stack.length = last_ptr |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
|
||||
|
proto.le = function(key) { |
||||
|
var cmp = this._compare |
||||
|
var n = this.root |
||||
|
var stack = [] |
||||
|
var last_ptr = 0 |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
stack.push(n) |
||||
|
if(d >= 0) { |
||||
|
last_ptr = stack.length |
||||
|
} |
||||
|
if(d < 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
stack.length = last_ptr |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
|
||||
|
//Finds the item with key if it exists
|
||||
|
proto.find = function(key) { |
||||
|
var cmp = this._compare |
||||
|
var n = this.root |
||||
|
var stack = [] |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
stack.push(n) |
||||
|
if(d === 0) { |
||||
|
return new RedBlackTreeIterator(this, stack) |
||||
|
} |
||||
|
if(d <= 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
return new RedBlackTreeIterator(this, []) |
||||
|
} |
||||
|
|
||||
|
//Removes item with key from tree
|
||||
|
proto.remove = function(key) { |
||||
|
var iter = this.find(key) |
||||
|
if(iter) { |
||||
|
return iter.remove() |
||||
|
} |
||||
|
return this |
||||
|
} |
||||
|
|
||||
|
//Returns the item at `key`
|
||||
|
proto.get = function(key) { |
||||
|
var cmp = this._compare |
||||
|
var n = this.root |
||||
|
while(n) { |
||||
|
var d = cmp(key, n.key) |
||||
|
if(d === 0) { |
||||
|
return n.value |
||||
|
} |
||||
|
if(d <= 0) { |
||||
|
n = n.left |
||||
|
} else { |
||||
|
n = n.right |
||||
|
} |
||||
|
} |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
//Iterator for red black tree
|
||||
|
function RedBlackTreeIterator(tree, stack) { |
||||
|
this.tree = tree |
||||
|
this._stack = stack |
||||
|
} |
||||
|
|
||||
|
var iproto = RedBlackTreeIterator.prototype |
||||
|
|
||||
|
//Test if iterator is valid
|
||||
|
Object.defineProperty(iproto, "valid", { |
||||
|
get: function() { |
||||
|
return this._stack.length > 0 |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Node of the iterator
|
||||
|
Object.defineProperty(iproto, "node", { |
||||
|
get: function() { |
||||
|
if(this._stack.length > 0) { |
||||
|
return this._stack[this._stack.length-1] |
||||
|
} |
||||
|
return null |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
//Makes a copy of an iterator
|
||||
|
iproto.clone = function() { |
||||
|
return new RedBlackTreeIterator(this.tree, this._stack.slice()) |
||||
|
} |
||||
|
|
||||
|
//Swaps two nodes
|
||||
|
function swapNode(n, v) { |
||||
|
n.key = v.key |
||||
|
n.value = v.value |
||||
|
n.left = v.left |
||||
|
n.right = v.right |
||||
|
n._color = v._color |
||||
|
n._count = v._count |
||||
|
} |
||||
|
|
||||
|
//Fix up a double black node in a tree
|
||||
|
function fixDoubleBlack(stack) { |
||||
|
var n, p, s, z |
||||
|
for(var i=stack.length-1; i>=0; --i) { |
||||
|
n = stack[i] |
||||
|
if(i === 0) { |
||||
|
n._color = BLACK |
||||
|
return |
||||
|
} |
||||
|
//console.log("visit node:", n.key, i, stack[i].key, stack[i-1].key)
|
||||
|
p = stack[i-1] |
||||
|
if(p.left === n) { |
||||
|
//console.log("left child")
|
||||
|
s = p.right |
||||
|
if(s.right && s.right._color === RED) { |
||||
|
//console.log("case 1: right sibling child red")
|
||||
|
s = p.right = cloneNode(s) |
||||
|
z = s.right = cloneNode(s.right) |
||||
|
p.right = s.left |
||||
|
s.left = p |
||||
|
s.right = z |
||||
|
s._color = p._color |
||||
|
n._color = BLACK |
||||
|
p._color = BLACK |
||||
|
z._color = BLACK |
||||
|
recount(p) |
||||
|
recount(s) |
||||
|
if(i > 1) { |
||||
|
var pp = stack[i-2] |
||||
|
if(pp.left === p) { |
||||
|
pp.left = s |
||||
|
} else { |
||||
|
pp.right = s |
||||
|
} |
||||
|
} |
||||
|
stack[i-1] = s |
||||
|
return |
||||
|
} else if(s.left && s.left._color === RED) { |
||||
|
//console.log("case 1: left sibling child red")
|
||||
|
s = p.right = cloneNode(s) |
||||
|
z = s.left = cloneNode(s.left) |
||||
|
p.right = z.left |
||||
|
s.left = z.right |
||||
|
z.left = p |
||||
|
z.right = s |
||||
|
z._color = p._color |
||||
|
p._color = BLACK |
||||
|
s._color = BLACK |
||||
|
n._color = BLACK |
||||
|
recount(p) |
||||
|
recount(s) |
||||
|
recount(z) |
||||
|
if(i > 1) { |
||||
|
var pp = stack[i-2] |
||||
|
if(pp.left === p) { |
||||
|
pp.left = z |
||||
|
} else { |
||||
|
pp.right = z |
||||
|
} |
||||
|
} |
||||
|
stack[i-1] = z |
||||
|
return |
||||
|
} |
||||
|
if(s._color === BLACK) { |
||||
|
if(p._color === RED) { |
||||
|
//console.log("case 2: black sibling, red parent", p.right.value)
|
||||
|
p._color = BLACK |
||||
|
p.right = repaint(RED, s) |
||||
|
return |
||||
|
} else { |
||||
|
//console.log("case 2: black sibling, black parent", p.right.value)
|
||||
|
p.right = repaint(RED, s) |
||||
|
continue |
||||
|
} |
||||
|
} else { |
||||
|
//console.log("case 3: red sibling")
|
||||
|
s = cloneNode(s) |
||||
|
p.right = s.left |
||||
|
s.left = p |
||||
|
s._color = p._color |
||||
|
p._color = RED |
||||
|
recount(p) |
||||
|
recount(s) |
||||
|
if(i > 1) { |
||||
|
var pp = stack[i-2] |
||||
|
if(pp.left === p) { |
||||
|
pp.left = s |
||||
|
} else { |
||||
|
pp.right = s |
||||
|
} |
||||
|
} |
||||
|
stack[i-1] = s |
||||
|
stack[i] = p |
||||
|
if(i+1 < stack.length) { |
||||
|
stack[i+1] = n |
||||
|
} else { |
||||
|
stack.push(n) |
||||
|
} |
||||
|
i = i+2 |
||||
|
} |
||||
|
} else { |
||||
|
//console.log("right child")
|
||||
|
s = p.left |
||||
|
if(s.left && s.left._color === RED) { |
||||
|
//console.log("case 1: left sibling child red", p.value, p._color)
|
||||
|
s = p.left = cloneNode(s) |
||||
|
z = s.left = cloneNode(s.left) |
||||
|
p.left = s.right |
||||
|
s.right = p |
||||
|
s.left = z |
||||
|
s._color = p._color |
||||
|
n._color = BLACK |
||||
|
p._color = BLACK |
||||
|
z._color = BLACK |
||||
|
recount(p) |
||||
|
recount(s) |
||||
|
if(i > 1) { |
||||
|
var pp = stack[i-2] |
||||
|
if(pp.right === p) { |
||||
|
pp.right = s |
||||
|
} else { |
||||
|
pp.left = s |
||||
|
} |
||||
|
} |
||||
|
stack[i-1] = s |
||||
|
return |
||||
|
} else if(s.right && s.right._color === RED) { |
||||
|
//console.log("case 1: right sibling child red")
|
||||
|
s = p.left = cloneNode(s) |
||||
|
z = s.right = cloneNode(s.right) |
||||
|
p.left = z.right |
||||
|
s.right = z.left |
||||
|
z.right = p |
||||
|
z.left = s |
||||
|
z._color = p._color |
||||
|
p._color = BLACK |
||||
|
s._color = BLACK |
||||
|
n._color = BLACK |
||||
|
recount(p) |
||||
|
recount(s) |
||||
|
recount(z) |
||||
|
if(i > 1) { |
||||
|
var pp = stack[i-2] |
||||
|
if(pp.right === p) { |
||||
|
pp.right = z |
||||
|
} else { |
||||
|
pp.left = z |
||||
|
} |
||||
|
} |
||||
|
stack[i-1] = z |
||||
|
return |
||||
|
} |
||||
|
if(s._color === BLACK) { |
||||
|
if(p._color === RED) { |
||||
|
//console.log("case 2: black sibling, red parent")
|
||||
|
p._color = BLACK |
||||
|
p.left = repaint(RED, s) |
||||
|
return |
||||
|
} else { |
||||
|
//console.log("case 2: black sibling, black parent")
|
||||
|
p.left = repaint(RED, s) |
||||
|
continue |
||||
|
} |
||||
|
} else { |
||||
|
//console.log("case 3: red sibling")
|
||||
|
s = cloneNode(s) |
||||
|
p.left = s.right |
||||
|
s.right = p |
||||
|
s._color = p._color |
||||
|
p._color = RED |
||||
|
recount(p) |
||||
|
recount(s) |
||||
|
if(i > 1) { |
||||
|
var pp = stack[i-2] |
||||
|
if(pp.right === p) { |
||||
|
pp.right = s |
||||
|
} else { |
||||
|
pp.left = s |
||||
|
} |
||||
|
} |
||||
|
stack[i-1] = s |
||||
|
stack[i] = p |
||||
|
if(i+1 < stack.length) { |
||||
|
stack[i+1] = n |
||||
|
} else { |
||||
|
stack.push(n) |
||||
|
} |
||||
|
i = i+2 |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Removes item at iterator from tree
|
||||
|
iproto.remove = function() { |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
return this.tree |
||||
|
} |
||||
|
//First copy path to node
|
||||
|
var cstack = new Array(stack.length) |
||||
|
var n = stack[stack.length-1] |
||||
|
cstack[cstack.length-1] = new RBNode(n._color, n.key, n.value, n.left, n.right, n._count) |
||||
|
for(var i=stack.length-2; i>=0; --i) { |
||||
|
var n = stack[i] |
||||
|
if(n.left === stack[i+1]) { |
||||
|
cstack[i] = new RBNode(n._color, n.key, n.value, cstack[i+1], n.right, n._count) |
||||
|
} else { |
||||
|
cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Get node
|
||||
|
n = cstack[cstack.length-1] |
||||
|
//console.log("start remove: ", n.value)
|
||||
|
|
||||
|
//If not leaf, then swap with previous node
|
||||
|
if(n.left && n.right) { |
||||
|
//console.log("moving to leaf")
|
||||
|
|
||||
|
//First walk to previous leaf
|
||||
|
var split = cstack.length |
||||
|
n = n.left |
||||
|
while(n.right) { |
||||
|
cstack.push(n) |
||||
|
n = n.right |
||||
|
} |
||||
|
//Copy path to leaf
|
||||
|
var v = cstack[split-1] |
||||
|
cstack.push(new RBNode(n._color, v.key, v.value, n.left, n.right, n._count)) |
||||
|
cstack[split-1].key = n.key |
||||
|
cstack[split-1].value = n.value |
||||
|
|
||||
|
//Fix up stack
|
||||
|
for(var i=cstack.length-2; i>=split; --i) { |
||||
|
n = cstack[i] |
||||
|
cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) |
||||
|
} |
||||
|
cstack[split-1].left = cstack[split] |
||||
|
} |
||||
|
//console.log("stack=", cstack.map(function(v) { return v.value }))
|
||||
|
|
||||
|
//Remove leaf node
|
||||
|
n = cstack[cstack.length-1] |
||||
|
if(n._color === RED) { |
||||
|
//Easy case: removing red leaf
|
||||
|
//console.log("RED leaf")
|
||||
|
var p = cstack[cstack.length-2] |
||||
|
if(p.left === n) { |
||||
|
p.left = null |
||||
|
} else if(p.right === n) { |
||||
|
p.right = null |
||||
|
} |
||||
|
cstack.pop() |
||||
|
for(var i=0; i<cstack.length; ++i) { |
||||
|
cstack[i]._count-- |
||||
|
} |
||||
|
return new RedBlackTree(this.tree._compare, cstack[0]) |
||||
|
} else { |
||||
|
if(n.left || n.right) { |
||||
|
//Second easy case: Single child black parent
|
||||
|
//console.log("BLACK single child")
|
||||
|
if(n.left) { |
||||
|
swapNode(n, n.left) |
||||
|
} else if(n.right) { |
||||
|
swapNode(n, n.right) |
||||
|
} |
||||
|
//Child must be red, so repaint it black to balance color
|
||||
|
n._color = BLACK |
||||
|
for(var i=0; i<cstack.length-1; ++i) { |
||||
|
cstack[i]._count-- |
||||
|
} |
||||
|
return new RedBlackTree(this.tree._compare, cstack[0]) |
||||
|
} else if(cstack.length === 1) { |
||||
|
//Third easy case: root
|
||||
|
//console.log("ROOT")
|
||||
|
return new RedBlackTree(this.tree._compare, null) |
||||
|
} else { |
||||
|
//Hard case: Repaint n, and then do some nasty stuff
|
||||
|
//console.log("BLACK leaf no children")
|
||||
|
for(var i=0; i<cstack.length; ++i) { |
||||
|
cstack[i]._count-- |
||||
|
} |
||||
|
var parent = cstack[cstack.length-2] |
||||
|
fixDoubleBlack(cstack) |
||||
|
//Fix up links
|
||||
|
if(parent.left === n) { |
||||
|
parent.left = null |
||||
|
} else { |
||||
|
parent.right = null |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return new RedBlackTree(this.tree._compare, cstack[0]) |
||||
|
} |
||||
|
|
||||
|
//Returns key
|
||||
|
Object.defineProperty(iproto, "key", { |
||||
|
get: function() { |
||||
|
if(this._stack.length > 0) { |
||||
|
return this._stack[this._stack.length-1].key |
||||
|
} |
||||
|
return |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
//Returns value
|
||||
|
Object.defineProperty(iproto, "value", { |
||||
|
get: function() { |
||||
|
if(this._stack.length > 0) { |
||||
|
return this._stack[this._stack.length-1].value |
||||
|
} |
||||
|
return |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
|
||||
|
//Returns the position of this iterator in the sorted list
|
||||
|
Object.defineProperty(iproto, "index", { |
||||
|
get: function() { |
||||
|
var idx = 0 |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
var r = this.tree.root |
||||
|
if(r) { |
||||
|
return r._count |
||||
|
} |
||||
|
return 0 |
||||
|
} else if(stack[stack.length-1].left) { |
||||
|
idx = stack[stack.length-1].left._count |
||||
|
} |
||||
|
for(var s=stack.length-2; s>=0; --s) { |
||||
|
if(stack[s+1] === stack[s].right) { |
||||
|
++idx |
||||
|
if(stack[s].left) { |
||||
|
idx += stack[s].left._count |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return idx |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
//Advances iterator to next element in list
|
||||
|
iproto.next = function() { |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
return |
||||
|
} |
||||
|
var n = stack[stack.length-1] |
||||
|
if(n.right) { |
||||
|
n = n.right |
||||
|
while(n) { |
||||
|
stack.push(n) |
||||
|
n = n.left |
||||
|
} |
||||
|
} else { |
||||
|
stack.pop() |
||||
|
while(stack.length > 0 && stack[stack.length-1].right === n) { |
||||
|
n = stack[stack.length-1] |
||||
|
stack.pop() |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Checks if iterator is at end of tree
|
||||
|
Object.defineProperty(iproto, "hasNext", { |
||||
|
get: function() { |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
return false |
||||
|
} |
||||
|
if(stack[stack.length-1].right) { |
||||
|
return true |
||||
|
} |
||||
|
for(var s=stack.length-1; s>0; --s) { |
||||
|
if(stack[s-1].left === stack[s]) { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Update value
|
||||
|
iproto.update = function(value) { |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
throw new Error("Can't update empty node!") |
||||
|
} |
||||
|
var cstack = new Array(stack.length) |
||||
|
var n = stack[stack.length-1] |
||||
|
cstack[cstack.length-1] = new RBNode(n._color, n.key, value, n.left, n.right, n._count) |
||||
|
for(var i=stack.length-2; i>=0; --i) { |
||||
|
n = stack[i] |
||||
|
if(n.left === stack[i+1]) { |
||||
|
cstack[i] = new RBNode(n._color, n.key, n.value, cstack[i+1], n.right, n._count) |
||||
|
} else { |
||||
|
cstack[i] = new RBNode(n._color, n.key, n.value, n.left, cstack[i+1], n._count) |
||||
|
} |
||||
|
} |
||||
|
return new RedBlackTree(this.tree._compare, cstack[0]) |
||||
|
} |
||||
|
|
||||
|
//Moves iterator backward one element
|
||||
|
iproto.prev = function() { |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
return |
||||
|
} |
||||
|
var n = stack[stack.length-1] |
||||
|
if(n.left) { |
||||
|
n = n.left |
||||
|
while(n) { |
||||
|
stack.push(n) |
||||
|
n = n.right |
||||
|
} |
||||
|
} else { |
||||
|
stack.pop() |
||||
|
while(stack.length > 0 && stack[stack.length-1].left === n) { |
||||
|
n = stack[stack.length-1] |
||||
|
stack.pop() |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Checks if iterator is at start of tree
|
||||
|
Object.defineProperty(iproto, "hasPrev", { |
||||
|
get: function() { |
||||
|
var stack = this._stack |
||||
|
if(stack.length === 0) { |
||||
|
return false |
||||
|
} |
||||
|
if(stack[stack.length-1].left) { |
||||
|
return true |
||||
|
} |
||||
|
for(var s=stack.length-1; s>0; --s) { |
||||
|
if(stack[s-1].right === stack[s]) { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
//Default comparison function
|
||||
|
function defaultCompare(a, b) { |
||||
|
if(a < b) { |
||||
|
return -1 |
||||
|
} |
||||
|
if(a > b) { |
||||
|
return 1 |
||||
|
} |
||||
|
return 0 |
||||
|
} |
||||
|
|
||||
|
//Build a tree
|
||||
|
function createRBTree(compare) { |
||||
|
return new RedBlackTree(compare || defaultCompare, null) |
||||
|
} |
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-11 |
|
||||
================== |
|
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-12 |
|
||||
================== |
|
@ -1,29 +1,12 @@ |
|||||
/** |
|
||||
* @author Titus Wormer |
|
||||
* @copyright 2016 Titus Wormer |
|
||||
* @license MIT |
|
||||
* @module is-alphanumerical |
|
||||
* @fileoverview Check if a character is alphanumerical. |
|
||||
*/ |
|
||||
|
|
||||
'use strict'; |
'use strict'; |
||||
|
|
||||
/* eslint-env commonjs */ |
|
||||
|
|
||||
/* Dependencies. */ |
|
||||
var alphabetical = require('is-alphabetical'); |
var alphabetical = require('is-alphabetical'); |
||||
var decimal = require('is-decimal'); |
var decimal = require('is-decimal'); |
||||
|
|
||||
/* Expose. */ |
|
||||
module.exports = alphanumerical; |
module.exports = alphanumerical; |
||||
|
|
||||
/** |
/* Check if the given character code, or the character |
||||
* Check whether the given character code, or the character |
* code at the first character, is alphanumerical. */ |
||||
* code at the first character, is alphanumerical. |
|
||||
* |
|
||||
* @param {string|number} character |
|
||||
* @return {boolean} - Whether `character` is alphanumerical. |
|
||||
*/ |
|
||||
function alphanumerical(character) { |
function alphanumerical(character) { |
||||
return alphabetical(character) || decimal(character); |
return alphabetical(character) || decimal(character); |
||||
} |
} |
||||
|
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-11 |
|
||||
================== |
|
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-11 |
|
||||
================== |
|
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-12 |
|
||||
================== |
|
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-12 |
|
||||
================== |
|
@ -0,0 +1,15 @@ |
|||||
|
The ISC License |
||||
|
|
||||
|
Copyright (c) Isaac Z. Schlueter and Contributors |
||||
|
|
||||
|
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. |
@ -0,0 +1,51 @@ |
|||||
|
# isexe |
||||
|
|
||||
|
Minimal module to check if a file is executable, and a normal file. |
||||
|
|
||||
|
Uses `fs.stat` and tests against the `PATHEXT` environment variable on |
||||
|
Windows. |
||||
|
|
||||
|
## USAGE |
||||
|
|
||||
|
```javascript |
||||
|
var isexe = require('isexe') |
||||
|
isexe('some-file-name', function (err, isExe) { |
||||
|
if (err) { |
||||
|
console.error('probably file does not exist or something', err) |
||||
|
} else if (isExe) { |
||||
|
console.error('this thing can be run') |
||||
|
} else { |
||||
|
console.error('cannot be run') |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
// same thing but synchronous, throws errors |
||||
|
var isExe = isexe.sync('some-file-name') |
||||
|
|
||||
|
// treat errors as just "not executable" |
||||
|
isexe('maybe-missing-file', { ignoreErrors: true }, callback) |
||||
|
var isExe = isexe.sync('maybe-missing-file', { ignoreErrors: true }) |
||||
|
``` |
||||
|
|
||||
|
## API |
||||
|
|
||||
|
### `isexe(path, [options], [callback])` |
||||
|
|
||||
|
Check if the path is executable. If no callback provided, and a |
||||
|
global `Promise` object is available, then a Promise will be returned. |
||||
|
|
||||
|
Will raise whatever errors may be raised by `fs.stat`, unless |
||||
|
`options.ignoreErrors` is set to true. |
||||
|
|
||||
|
### `isexe.sync(path, [options])` |
||||
|
|
||||
|
Same as `isexe` but returns the value and throws any errors raised. |
||||
|
|
||||
|
### Options |
||||
|
|
||||
|
* `ignoreErrors` Treat all errors as "no, this is not executable", but |
||||
|
don't raise them. |
||||
|
* `uid` Number to use as the user id |
||||
|
* `gid` Number to use as the group id |
||||
|
* `pathExt` List of path extensions to use instead of `PATHEXT` |
||||
|
environment variable on Windows. |
@ -0,0 +1,57 @@ |
|||||
|
var fs = require('fs') |
||||
|
var core |
||||
|
if (process.platform === 'win32' || global.TESTING_WINDOWS) { |
||||
|
core = require('./windows.js') |
||||
|
} else { |
||||
|
core = require('./mode.js') |
||||
|
} |
||||
|
|
||||
|
module.exports = isexe |
||||
|
isexe.sync = sync |
||||
|
|
||||
|
function isexe (path, options, cb) { |
||||
|
if (typeof options === 'function') { |
||||
|
cb = options |
||||
|
options = {} |
||||
|
} |
||||
|
|
||||
|
if (!cb) { |
||||
|
if (typeof Promise !== 'function') { |
||||
|
throw new TypeError('callback not provided') |
||||
|
} |
||||
|
|
||||
|
return new Promise(function (resolve, reject) { |
||||
|
isexe(path, options || {}, function (er, is) { |
||||
|
if (er) { |
||||
|
reject(er) |
||||
|
} else { |
||||
|
resolve(is) |
||||
|
} |
||||
|
}) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
core(path, options || {}, function (er, is) { |
||||
|
// ignore EACCES because that just means we aren't allowed to run it
|
||||
|
if (er) { |
||||
|
if (er.code === 'EACCES' || options && options.ignoreErrors) { |
||||
|
er = null |
||||
|
is = false |
||||
|
} |
||||
|
} |
||||
|
cb(er, is) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
function sync (path, options) { |
||||
|
// my kingdom for a filtered catch
|
||||
|
try { |
||||
|
return core.sync(path, options || {}) |
||||
|
} catch (er) { |
||||
|
if (options && options.ignoreErrors || er.code === 'EACCES') { |
||||
|
return false |
||||
|
} else { |
||||
|
throw er |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
module.exports = isexe |
||||
|
isexe.sync = sync |
||||
|
|
||||
|
var fs = require('fs') |
||||
|
|
||||
|
function isexe (path, options, cb) { |
||||
|
fs.stat(path, function (er, stat) { |
||||
|
cb(er, er ? false : checkStat(stat, options)) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
function sync (path, options) { |
||||
|
return checkStat(fs.statSync(path), options) |
||||
|
} |
||||
|
|
||||
|
function checkStat (stat, options) { |
||||
|
return stat.isFile() && checkMode(stat, options) |
||||
|
} |
||||
|
|
||||
|
function checkMode (stat, options) { |
||||
|
var mod = stat.mode |
||||
|
var uid = stat.uid |
||||
|
var gid = stat.gid |
||||
|
|
||||
|
var myUid = options.uid !== undefined ? |
||||
|
options.uid : process.getuid && process.getuid() |
||||
|
var myGid = options.gid !== undefined ? |
||||
|
options.gid : process.getgid && process.getgid() |
||||
|
|
||||
|
var u = parseInt('100', 8) |
||||
|
var g = parseInt('010', 8) |
||||
|
var o = parseInt('001', 8) |
||||
|
var ug = u | g |
||||
|
|
||||
|
var ret = (mod & o) || |
||||
|
(mod & g) && gid === myGid || |
||||
|
(mod & u) && uid === myUid || |
||||
|
(mod & ug) && myUid === 0 |
||||
|
|
||||
|
return ret |
||||
|
} |
@ -0,0 +1,60 @@ |
|||||
|
{ |
||||
|
"_from": "isexe@^2.0.0", |
||||
|
"_id": "isexe@2.0.0", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", |
||||
|
"_location": "/eslint/isexe", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "isexe@^2.0.0", |
||||
|
"name": "isexe", |
||||
|
"escapedName": "isexe", |
||||
|
"rawSpec": "^2.0.0", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^2.0.0" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint/which" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", |
||||
|
"_shasum": "e8fbf374dc556ff8947a10dcb0572d633f2cfa10", |
||||
|
"_spec": "isexe@^2.0.0", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint/node_modules/which", |
||||
|
"author": { |
||||
|
"name": "Isaac Z. Schlueter", |
||||
|
"email": "i@izs.me", |
||||
|
"url": "http://blog.izs.me/" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/isaacs/isexe/issues" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"deprecated": false, |
||||
|
"description": "Minimal module to check if a file is executable.", |
||||
|
"devDependencies": { |
||||
|
"mkdirp": "^0.5.1", |
||||
|
"rimraf": "^2.5.0", |
||||
|
"tap": "^10.3.0" |
||||
|
}, |
||||
|
"directories": { |
||||
|
"test": "test" |
||||
|
}, |
||||
|
"homepage": "https://github.com/isaacs/isexe#readme", |
||||
|
"keywords": [], |
||||
|
"license": "ISC", |
||||
|
"main": "index.js", |
||||
|
"name": "isexe", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git+https://github.com/isaacs/isexe.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"postpublish": "git push origin --all; git push origin --tags", |
||||
|
"postversion": "npm publish", |
||||
|
"preversion": "npm test", |
||||
|
"test": "tap test/*.js --100" |
||||
|
}, |
||||
|
"version": "2.0.0" |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
module.exports = isexe |
||||
|
isexe.sync = sync |
||||
|
|
||||
|
var fs = require('fs') |
||||
|
|
||||
|
function checkPathExt (path, options) { |
||||
|
var pathext = options.pathExt !== undefined ? |
||||
|
options.pathExt : process.env.PATHEXT |
||||
|
|
||||
|
if (!pathext) { |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
pathext = pathext.split(';') |
||||
|
if (pathext.indexOf('') !== -1) { |
||||
|
return true |
||||
|
} |
||||
|
for (var i = 0; i < pathext.length; i++) { |
||||
|
var p = pathext[i].toLowerCase() |
||||
|
if (p && path.substr(-p.length).toLowerCase() === p) { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
function checkStat (stat, path, options) { |
||||
|
if (!stat.isSymbolicLink() && !stat.isFile()) { |
||||
|
return false |
||||
|
} |
||||
|
return checkPathExt(path, options) |
||||
|
} |
||||
|
|
||||
|
function isexe (path, options, cb) { |
||||
|
fs.stat(path, function (er, stat) { |
||||
|
cb(er, er ? false : checkStat(stat, path, options)) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
function sync (path, options) { |
||||
|
return checkStat(fs.statSync(path), path, options) |
||||
|
} |
@ -0,0 +1,504 @@ |
|||||
|
GNU LESSER GENERAL PUBLIC LICENSE |
||||
|
Version 2.1, February 1999 |
||||
|
|
||||
|
Copyright (C) 1991, 1999 Free Software Foundation, Inc. |
||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
|
Everyone is permitted to copy and distribute verbatim copies |
||||
|
of this license document, but changing it is not allowed. |
||||
|
|
||||
|
(This is the first released version of the Lesser GPL. It also counts |
||||
|
as the successor of the GNU Library Public License, version 2, hence |
||||
|
the version number 2.1.) |
||||
|
|
||||
|
Preamble |
||||
|
|
||||
|
The licenses for most software are designed to take away your |
||||
|
freedom to share and change it. By contrast, the GNU General Public |
||||
|
Licenses are intended to guarantee your freedom to share and change |
||||
|
free software--to make sure the software is free for all its users. |
||||
|
|
||||
|
This license, the Lesser General Public License, applies to some |
||||
|
specially designated software packages--typically libraries--of the |
||||
|
Free Software Foundation and other authors who decide to use it. You |
||||
|
can use it too, but we suggest you first think carefully about whether |
||||
|
this license or the ordinary General Public License is the better |
||||
|
strategy to use in any particular case, based on the explanations below. |
||||
|
|
||||
|
When we speak of free software, we are referring to freedom of use, |
||||
|
not price. Our General Public Licenses are designed to make sure that |
||||
|
you have the freedom to distribute copies of free software (and charge |
||||
|
for this service if you wish); that you receive source code or can get |
||||
|
it if you want it; that you can change the software and use pieces of |
||||
|
it in new free programs; and that you are informed that you can do |
||||
|
these things. |
||||
|
|
||||
|
To protect your rights, we need to make restrictions that forbid |
||||
|
distributors to deny you these rights or to ask you to surrender these |
||||
|
rights. These restrictions translate to certain responsibilities for |
||||
|
you if you distribute copies of the library or if you modify it. |
||||
|
|
||||
|
For example, if you distribute copies of the library, whether gratis |
||||
|
or for a fee, you must give the recipients all the rights that we gave |
||||
|
you. You must make sure that they, too, receive or can get the source |
||||
|
code. If you link other code with the library, you must provide |
||||
|
complete object files to the recipients, so that they can relink them |
||||
|
with the library after making changes to the library and recompiling |
||||
|
it. And you must show them these terms so they know their rights. |
||||
|
|
||||
|
We protect your rights with a two-step method: (1) we copyright the |
||||
|
library, and (2) we offer you this license, which gives you legal |
||||
|
permission to copy, distribute and/or modify the library. |
||||
|
|
||||
|
To protect each distributor, we want to make it very clear that |
||||
|
there is no warranty for the free library. Also, if the library is |
||||
|
modified by someone else and passed on, the recipients should know |
||||
|
that what they have is not the original version, so that the original |
||||
|
author's reputation will not be affected by problems that might be |
||||
|
introduced by others. |
||||
|
|
||||
|
Finally, software patents pose a constant threat to the existence of |
||||
|
any free program. We wish to make sure that a company cannot |
||||
|
effectively restrict the users of a free program by obtaining a |
||||
|
restrictive license from a patent holder. Therefore, we insist that |
||||
|
any patent license obtained for a version of the library must be |
||||
|
consistent with the full freedom of use specified in this license. |
||||
|
|
||||
|
Most GNU software, including some libraries, is covered by the |
||||
|
ordinary GNU General Public License. This license, the GNU Lesser |
||||
|
General Public License, applies to certain designated libraries, and |
||||
|
is quite different from the ordinary General Public License. We use |
||||
|
this license for certain libraries in order to permit linking those |
||||
|
libraries into non-free programs. |
||||
|
|
||||
|
When a program is linked with a library, whether statically or using |
||||
|
a shared library, the combination of the two is legally speaking a |
||||
|
combined work, a derivative of the original library. The ordinary |
||||
|
General Public License therefore permits such linking only if the |
||||
|
entire combination fits its criteria of freedom. The Lesser General |
||||
|
Public License permits more lax criteria for linking other code with |
||||
|
the library. |
||||
|
|
||||
|
We call this license the "Lesser" General Public License because it |
||||
|
does Less to protect the user's freedom than the ordinary General |
||||
|
Public License. It also provides other free software developers Less |
||||
|
of an advantage over competing non-free programs. These disadvantages |
||||
|
are the reason we use the ordinary General Public License for many |
||||
|
libraries. However, the Lesser license provides advantages in certain |
||||
|
special circumstances. |
||||
|
|
||||
|
For example, on rare occasions, there may be a special need to |
||||
|
encourage the widest possible use of a certain library, so that it becomes |
||||
|
a de-facto standard. To achieve this, non-free programs must be |
||||
|
allowed to use the library. A more frequent case is that a free |
||||
|
library does the same job as widely used non-free libraries. In this |
||||
|
case, there is little to gain by limiting the free library to free |
||||
|
software only, so we use the Lesser General Public License. |
||||
|
|
||||
|
In other cases, permission to use a particular library in non-free |
||||
|
programs enables a greater number of people to use a large body of |
||||
|
free software. For example, permission to use the GNU C Library in |
||||
|
non-free programs enables many more people to use the whole GNU |
||||
|
operating system, as well as its variant, the GNU/Linux operating |
||||
|
system. |
||||
|
|
||||
|
Although the Lesser General Public License is Less protective of the |
||||
|
users' freedom, it does ensure that the user of a program that is |
||||
|
linked with the Library has the freedom and the wherewithal to run |
||||
|
that program using a modified version of the Library. |
||||
|
|
||||
|
The precise terms and conditions for copying, distribution and |
||||
|
modification follow. Pay close attention to the difference between a |
||||
|
"work based on the library" and a "work that uses the library". The |
||||
|
former contains code derived from the library, whereas the latter must |
||||
|
be combined with the library in order to run. |
||||
|
|
||||
|
GNU LESSER GENERAL PUBLIC LICENSE |
||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION |
||||
|
|
||||
|
0. This License Agreement applies to any software library or other |
||||
|
program which contains a notice placed by the copyright holder or |
||||
|
other authorized party saying it may be distributed under the terms of |
||||
|
this Lesser General Public License (also called "this License"). |
||||
|
Each licensee is addressed as "you". |
||||
|
|
||||
|
A "library" means a collection of software functions and/or data |
||||
|
prepared so as to be conveniently linked with application programs |
||||
|
(which use some of those functions and data) to form executables. |
||||
|
|
||||
|
The "Library", below, refers to any such software library or work |
||||
|
which has been distributed under these terms. A "work based on the |
||||
|
Library" means either the Library or any derivative work under |
||||
|
copyright law: that is to say, a work containing the Library or a |
||||
|
portion of it, either verbatim or with modifications and/or translated |
||||
|
straightforwardly into another language. (Hereinafter, translation is |
||||
|
included without limitation in the term "modification".) |
||||
|
|
||||
|
"Source code" for a work means the preferred form of the work for |
||||
|
making modifications to it. For a library, complete source code means |
||||
|
all the source code for all modules it contains, plus any associated |
||||
|
interface definition files, plus the scripts used to control compilation |
||||
|
and installation of the library. |
||||
|
|
||||
|
Activities other than copying, distribution and modification are not |
||||
|
covered by this License; they are outside its scope. The act of |
||||
|
running a program using the Library is not restricted, and output from |
||||
|
such a program is covered only if its contents constitute a work based |
||||
|
on the Library (independent of the use of the Library in a tool for |
||||
|
writing it). Whether that is true depends on what the Library does |
||||
|
and what the program that uses the Library does. |
||||
|
|
||||
|
1. You may copy and distribute verbatim copies of the Library's |
||||
|
complete source code as you receive it, in any medium, provided that |
||||
|
you conspicuously and appropriately publish on each copy an |
||||
|
appropriate copyright notice and disclaimer of warranty; keep intact |
||||
|
all the notices that refer to this License and to the absence of any |
||||
|
warranty; and distribute a copy of this License along with the |
||||
|
Library. |
||||
|
|
||||
|
You may charge a fee for the physical act of transferring a copy, |
||||
|
and you may at your option offer warranty protection in exchange for a |
||||
|
fee. |
||||
|
|
||||
|
2. You may modify your copy or copies of the Library or any portion |
||||
|
of it, thus forming a work based on the Library, and copy and |
||||
|
distribute such modifications or work under the terms of Section 1 |
||||
|
above, provided that you also meet all of these conditions: |
||||
|
|
||||
|
a) The modified work must itself be a software library. |
||||
|
|
||||
|
b) You must cause the files modified to carry prominent notices |
||||
|
stating that you changed the files and the date of any change. |
||||
|
|
||||
|
c) You must cause the whole of the work to be licensed at no |
||||
|
charge to all third parties under the terms of this License. |
||||
|
|
||||
|
d) If a facility in the modified Library refers to a function or a |
||||
|
table of data to be supplied by an application program that uses |
||||
|
the facility, other than as an argument passed when the facility |
||||
|
is invoked, then you must make a good faith effort to ensure that, |
||||
|
in the event an application does not supply such function or |
||||
|
table, the facility still operates, and performs whatever part of |
||||
|
its purpose remains meaningful. |
||||
|
|
||||
|
(For example, a function in a library to compute square roots has |
||||
|
a purpose that is entirely well-defined independent of the |
||||
|
application. Therefore, Subsection 2d requires that any |
||||
|
application-supplied function or table used by this function must |
||||
|
be optional: if the application does not supply it, the square |
||||
|
root function must still compute square roots.) |
||||
|
|
||||
|
These requirements apply to the modified work as a whole. If |
||||
|
identifiable sections of that work are not derived from the Library, |
||||
|
and can be reasonably considered independent and separate works in |
||||
|
themselves, then this License, and its terms, do not apply to those |
||||
|
sections when you distribute them as separate works. But when you |
||||
|
distribute the same sections as part of a whole which is a work based |
||||
|
on the Library, the distribution of the whole must be on the terms of |
||||
|
this License, whose permissions for other licensees extend to the |
||||
|
entire whole, and thus to each and every part regardless of who wrote |
||||
|
it. |
||||
|
|
||||
|
Thus, it is not the intent of this section to claim rights or contest |
||||
|
your rights to work written entirely by you; rather, the intent is to |
||||
|
exercise the right to control the distribution of derivative or |
||||
|
collective works based on the Library. |
||||
|
|
||||
|
In addition, mere aggregation of another work not based on the Library |
||||
|
with the Library (or with a work based on the Library) on a volume of |
||||
|
a storage or distribution medium does not bring the other work under |
||||
|
the scope of this License. |
||||
|
|
||||
|
3. You may opt to apply the terms of the ordinary GNU General Public |
||||
|
License instead of this License to a given copy of the Library. To do |
||||
|
this, you must alter all the notices that refer to this License, so |
||||
|
that they refer to the ordinary GNU General Public License, version 2, |
||||
|
instead of to this License. (If a newer version than version 2 of the |
||||
|
ordinary GNU General Public License has appeared, then you can specify |
||||
|
that version instead if you wish.) Do not make any other change in |
||||
|
these notices. |
||||
|
|
||||
|
Once this change is made in a given copy, it is irreversible for |
||||
|
that copy, so the ordinary GNU General Public License applies to all |
||||
|
subsequent copies and derivative works made from that copy. |
||||
|
|
||||
|
This option is useful when you wish to copy part of the code of |
||||
|
the Library into a program that is not a library. |
||||
|
|
||||
|
4. You may copy and distribute the Library (or a portion or |
||||
|
derivative of it, under Section 2) in object code or executable form |
||||
|
under the terms of Sections 1 and 2 above provided that you accompany |
||||
|
it with the complete corresponding machine-readable source code, which |
||||
|
must be distributed under the terms of Sections 1 and 2 above on a |
||||
|
medium customarily used for software interchange. |
||||
|
|
||||
|
If distribution of object code is made by offering access to copy |
||||
|
from a designated place, then offering equivalent access to copy the |
||||
|
source code from the same place satisfies the requirement to |
||||
|
distribute the source code, even though third parties are not |
||||
|
compelled to copy the source along with the object code. |
||||
|
|
||||
|
5. A program that contains no derivative of any portion of the |
||||
|
Library, but is designed to work with the Library by being compiled or |
||||
|
linked with it, is called a "work that uses the Library". Such a |
||||
|
work, in isolation, is not a derivative work of the Library, and |
||||
|
therefore falls outside the scope of this License. |
||||
|
|
||||
|
However, linking a "work that uses the Library" with the Library |
||||
|
creates an executable that is a derivative of the Library (because it |
||||
|
contains portions of the Library), rather than a "work that uses the |
||||
|
library". The executable is therefore covered by this License. |
||||
|
Section 6 states terms for distribution of such executables. |
||||
|
|
||||
|
When a "work that uses the Library" uses material from a header file |
||||
|
that is part of the Library, the object code for the work may be a |
||||
|
derivative work of the Library even though the source code is not. |
||||
|
Whether this is true is especially significant if the work can be |
||||
|
linked without the Library, or if the work is itself a library. The |
||||
|
threshold for this to be true is not precisely defined by law. |
||||
|
|
||||
|
If such an object file uses only numerical parameters, data |
||||
|
structure layouts and accessors, and small macros and small inline |
||||
|
functions (ten lines or less in length), then the use of the object |
||||
|
file is unrestricted, regardless of whether it is legally a derivative |
||||
|
work. (Executables containing this object code plus portions of the |
||||
|
Library will still fall under Section 6.) |
||||
|
|
||||
|
Otherwise, if the work is a derivative of the Library, you may |
||||
|
distribute the object code for the work under the terms of Section 6. |
||||
|
Any executables containing that work also fall under Section 6, |
||||
|
whether or not they are linked directly with the Library itself. |
||||
|
|
||||
|
6. As an exception to the Sections above, you may also combine or |
||||
|
link a "work that uses the Library" with the Library to produce a |
||||
|
work containing portions of the Library, and distribute that work |
||||
|
under terms of your choice, provided that the terms permit |
||||
|
modification of the work for the customer's own use and reverse |
||||
|
engineering for debugging such modifications. |
||||
|
|
||||
|
You must give prominent notice with each copy of the work that the |
||||
|
Library is used in it and that the Library and its use are covered by |
||||
|
this License. You must supply a copy of this License. If the work |
||||
|
during execution displays copyright notices, you must include the |
||||
|
copyright notice for the Library among them, as well as a reference |
||||
|
directing the user to the copy of this License. Also, you must do one |
||||
|
of these things: |
||||
|
|
||||
|
a) Accompany the work with the complete corresponding |
||||
|
machine-readable source code for the Library including whatever |
||||
|
changes were used in the work (which must be distributed under |
||||
|
Sections 1 and 2 above); and, if the work is an executable linked |
||||
|
with the Library, with the complete machine-readable "work that |
||||
|
uses the Library", as object code and/or source code, so that the |
||||
|
user can modify the Library and then relink to produce a modified |
||||
|
executable containing the modified Library. (It is understood |
||||
|
that the user who changes the contents of definitions files in the |
||||
|
Library will not necessarily be able to recompile the application |
||||
|
to use the modified definitions.) |
||||
|
|
||||
|
b) Use a suitable shared library mechanism for linking with the |
||||
|
Library. A suitable mechanism is one that (1) uses at run time a |
||||
|
copy of the library already present on the user's computer system, |
||||
|
rather than copying library functions into the executable, and (2) |
||||
|
will operate properly with a modified version of the library, if |
||||
|
the user installs one, as long as the modified version is |
||||
|
interface-compatible with the version that the work was made with. |
||||
|
|
||||
|
c) Accompany the work with a written offer, valid for at |
||||
|
least three years, to give the same user the materials |
||||
|
specified in Subsection 6a, above, for a charge no more |
||||
|
than the cost of performing this distribution. |
||||
|
|
||||
|
d) If distribution of the work is made by offering access to copy |
||||
|
from a designated place, offer equivalent access to copy the above |
||||
|
specified materials from the same place. |
||||
|
|
||||
|
e) Verify that the user has already received a copy of these |
||||
|
materials or that you have already sent this user a copy. |
||||
|
|
||||
|
For an executable, the required form of the "work that uses the |
||||
|
Library" must include any data and utility programs needed for |
||||
|
reproducing the executable from it. However, as a special exception, |
||||
|
the materials to be distributed need not include anything that is |
||||
|
normally distributed (in either source or binary form) with the major |
||||
|
components (compiler, kernel, and so on) of the operating system on |
||||
|
which the executable runs, unless that component itself accompanies |
||||
|
the executable. |
||||
|
|
||||
|
It may happen that this requirement contradicts the license |
||||
|
restrictions of other proprietary libraries that do not normally |
||||
|
accompany the operating system. Such a contradiction means you cannot |
||||
|
use both them and the Library together in an executable that you |
||||
|
distribute. |
||||
|
|
||||
|
7. You may place library facilities that are a work based on the |
||||
|
Library side-by-side in a single library together with other library |
||||
|
facilities not covered by this License, and distribute such a combined |
||||
|
library, provided that the separate distribution of the work based on |
||||
|
the Library and of the other library facilities is otherwise |
||||
|
permitted, and provided that you do these two things: |
||||
|
|
||||
|
a) Accompany the combined library with a copy of the same work |
||||
|
based on the Library, uncombined with any other library |
||||
|
facilities. This must be distributed under the terms of the |
||||
|
Sections above. |
||||
|
|
||||
|
b) Give prominent notice with the combined library of the fact |
||||
|
that part of it is a work based on the Library, and explaining |
||||
|
where to find the accompanying uncombined form of the same work. |
||||
|
|
||||
|
8. You may not copy, modify, sublicense, link with, or distribute |
||||
|
the Library except as expressly provided under this License. Any |
||||
|
attempt otherwise to copy, modify, sublicense, link with, or |
||||
|
distribute the Library is void, and will automatically terminate your |
||||
|
rights under this License. However, parties who have received copies, |
||||
|
or rights, from you under this License will not have their licenses |
||||
|
terminated so long as such parties remain in full compliance. |
||||
|
|
||||
|
9. You are not required to accept this License, since you have not |
||||
|
signed it. However, nothing else grants you permission to modify or |
||||
|
distribute the Library or its derivative works. These actions are |
||||
|
prohibited by law if you do not accept this License. Therefore, by |
||||
|
modifying or distributing the Library (or any work based on the |
||||
|
Library), you indicate your acceptance of this License to do so, and |
||||
|
all its terms and conditions for copying, distributing or modifying |
||||
|
the Library or works based on it. |
||||
|
|
||||
|
10. Each time you redistribute the Library (or any work based on the |
||||
|
Library), the recipient automatically receives a license from the |
||||
|
original licensor to copy, distribute, link with or modify the Library |
||||
|
subject to these terms and conditions. You may not impose any further |
||||
|
restrictions on the recipients' exercise of the rights granted herein. |
||||
|
You are not responsible for enforcing compliance by third parties with |
||||
|
this License. |
||||
|
|
||||
|
11. If, as a consequence of a court judgment or allegation of patent |
||||
|
infringement or for any other reason (not limited to patent issues), |
||||
|
conditions are imposed on you (whether by court order, agreement or |
||||
|
otherwise) that contradict the conditions of this License, they do not |
||||
|
excuse you from the conditions of this License. If you cannot |
||||
|
distribute so as to satisfy simultaneously your obligations under this |
||||
|
License and any other pertinent obligations, then as a consequence you |
||||
|
may not distribute the Library at all. For example, if a patent |
||||
|
license would not permit royalty-free redistribution of the Library by |
||||
|
all those who receive copies directly or indirectly through you, then |
||||
|
the only way you could satisfy both it and this License would be to |
||||
|
refrain entirely from distribution of the Library. |
||||
|
|
||||
|
If any portion of this section is held invalid or unenforceable under any |
||||
|
particular circumstance, the balance of the section is intended to apply, |
||||
|
and the section as a whole is intended to apply in other circumstances. |
||||
|
|
||||
|
It is not the purpose of this section to induce you to infringe any |
||||
|
patents or other property right claims or to contest validity of any |
||||
|
such claims; this section has the sole purpose of protecting the |
||||
|
integrity of the free software distribution system which is |
||||
|
implemented by public license practices. Many people have made |
||||
|
generous contributions to the wide range of software distributed |
||||
|
through that system in reliance on consistent application of that |
||||
|
system; it is up to the author/donor to decide if he or she is willing |
||||
|
to distribute software through any other system and a licensee cannot |
||||
|
impose that choice. |
||||
|
|
||||
|
This section is intended to make thoroughly clear what is believed to |
||||
|
be a consequence of the rest of this License. |
||||
|
|
||||
|
12. If the distribution and/or use of the Library is restricted in |
||||
|
certain countries either by patents or by copyrighted interfaces, the |
||||
|
original copyright holder who places the Library under this License may add |
||||
|
an explicit geographical distribution limitation excluding those countries, |
||||
|
so that distribution is permitted only in or among countries not thus |
||||
|
excluded. In such case, this License incorporates the limitation as if |
||||
|
written in the body of this License. |
||||
|
|
||||
|
13. The Free Software Foundation may publish revised and/or new |
||||
|
versions of the Lesser General Public License from time to time. |
||||
|
Such new versions will be similar in spirit to the present version, |
||||
|
but may differ in detail to address new problems or concerns. |
||||
|
|
||||
|
Each version is given a distinguishing version number. If the Library |
||||
|
specifies a version number of this License which applies to it and |
||||
|
"any later version", you have the option of following the terms and |
||||
|
conditions either of that version or of any later version published by |
||||
|
the Free Software Foundation. If the Library does not specify a |
||||
|
license version number, you may choose any version ever published by |
||||
|
the Free Software Foundation. |
||||
|
|
||||
|
14. If you wish to incorporate parts of the Library into other free |
||||
|
programs whose distribution conditions are incompatible with these, |
||||
|
write to the author to ask for permission. For software which is |
||||
|
copyrighted by the Free Software Foundation, write to the Free |
||||
|
Software Foundation; we sometimes make exceptions for this. Our |
||||
|
decision will be guided by the two goals of preserving the free status |
||||
|
of all derivatives of our free software and of promoting the sharing |
||||
|
and reuse of software generally. |
||||
|
|
||||
|
NO WARRANTY |
||||
|
|
||||
|
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO |
||||
|
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. |
||||
|
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR |
||||
|
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY |
||||
|
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE |
||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE |
||||
|
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME |
||||
|
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
||||
|
|
||||
|
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN |
||||
|
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY |
||||
|
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU |
||||
|
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR |
||||
|
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE |
||||
|
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
||||
|
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
||||
|
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
||||
|
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
||||
|
DAMAGES. |
||||
|
|
||||
|
END OF TERMS AND CONDITIONS |
||||
|
|
||||
|
How to Apply These Terms to Your New Libraries |
||||
|
|
||||
|
If you develop a new library, and you want it to be of the greatest |
||||
|
possible use to the public, we recommend making it free software that |
||||
|
everyone can redistribute and change. You can do so by permitting |
||||
|
redistribution under these terms (or, alternatively, under the terms of the |
||||
|
ordinary General Public License). |
||||
|
|
||||
|
To apply these terms, attach the following notices to the library. It is |
||||
|
safest to attach them to the start of each source file to most effectively |
||||
|
convey the exclusion of warranty; and each file should have at least the |
||||
|
"copyright" line and a pointer to where the full notice is found. |
||||
|
|
||||
|
{description} |
||||
|
Copyright (C) {year} {fullname} |
||||
|
|
||||
|
This library is free software; you can redistribute it and/or |
||||
|
modify it under the terms of the GNU Lesser General Public |
||||
|
License as published by the Free Software Foundation; either |
||||
|
version 2.1 of the License, or (at your option) any later version. |
||||
|
|
||||
|
This library is distributed in the hope that it will be useful, |
||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||
|
Lesser General Public License for more details. |
||||
|
|
||||
|
You should have received a copy of the GNU Lesser General Public |
||||
|
License along with this library; if not, write to the Free Software |
||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
||||
|
USA |
||||
|
|
||||
|
Also add information on how to contact you by electronic and paper mail. |
||||
|
|
||||
|
You should also get your employer (if you work as a programmer) or your |
||||
|
school, if any, to sign a "copyright disclaimer" for the library, if |
||||
|
necessary. Here is a sample; alter the names: |
||||
|
|
||||
|
Yoyodyne, Inc., hereby disclaims all copyright interest in the |
||||
|
library `Frob' (a library for tweaking knobs) written by James Random |
||||
|
Hacker. |
||||
|
|
||||
|
{signature of Ty Coon}, 1 April 1990 |
||||
|
Ty Coon, President of Vice |
||||
|
|
||||
|
That's all there is to it! |
File diff suppressed because it is too large
@ -0,0 +1,15 @@ |
|||||
|
The ISC License |
||||
|
|
||||
|
Copyright (c) Isaac Z. Schlueter and Contributors |
||||
|
|
||||
|
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. |
@ -0,0 +1,152 @@ |
|||||
|
# lru cache |
||||
|
|
||||
|
A cache object that deletes the least-recently-used items. |
||||
|
|
||||
|
[![Build Status](https://travis-ci.org/isaacs/node-lru-cache.svg?branch=master)](https://travis-ci.org/isaacs/node-lru-cache) [![Coverage Status](https://coveralls.io/repos/isaacs/node-lru-cache/badge.svg?service=github)](https://coveralls.io/github/isaacs/node-lru-cache) |
||||
|
|
||||
|
## Installation: |
||||
|
|
||||
|
```javascript |
||||
|
npm install lru-cache --save |
||||
|
``` |
||||
|
|
||||
|
## Usage: |
||||
|
|
||||
|
```javascript |
||||
|
var LRU = require("lru-cache") |
||||
|
, options = { max: 500 |
||||
|
, length: function (n, key) { return n * 2 + key.length } |
||||
|
, dispose: function (key, n) { n.close() } |
||||
|
, maxAge: 1000 * 60 * 60 } |
||||
|
, cache = LRU(options) |
||||
|
, otherCache = LRU(50) // sets just the max size |
||||
|
|
||||
|
cache.set("key", "value") |
||||
|
cache.get("key") // "value" |
||||
|
|
||||
|
// non-string keys ARE fully supported |
||||
|
var someObject = {} |
||||
|
cache.set(someObject, 'a value') |
||||
|
cache.set('[object Object]', 'a different value') |
||||
|
assert.equal(cache.get(someObject), 'a value') |
||||
|
|
||||
|
cache.reset() // empty the cache |
||||
|
``` |
||||
|
|
||||
|
If you put more stuff in it, then items will fall out. |
||||
|
|
||||
|
If you try to put an oversized thing in it, then it'll fall out right |
||||
|
away. |
||||
|
|
||||
|
## Options |
||||
|
|
||||
|
* `max` The maximum size of the cache, checked by applying the length |
||||
|
function to all values in the cache. Not setting this is kind of |
||||
|
silly, since that's the whole purpose of this lib, but it defaults |
||||
|
to `Infinity`. |
||||
|
* `maxAge` Maximum age in ms. Items are not pro-actively pruned out |
||||
|
as they age, but if you try to get an item that is too old, it'll |
||||
|
drop it and return undefined instead of giving it to you. |
||||
|
* `length` Function that is used to calculate the length of stored |
||||
|
items. If you're storing strings or buffers, then you probably want |
||||
|
to do something like `function(n, key){return n.length}`. The default is |
||||
|
`function(){return 1}`, which is fine if you want to store `max` |
||||
|
like-sized things. The item is passed as the first argument, and |
||||
|
the key is passed as the second argumnet. |
||||
|
* `dispose` Function that is called on items when they are dropped |
||||
|
from the cache. This can be handy if you want to close file |
||||
|
descriptors or do other cleanup tasks when items are no longer |
||||
|
accessible. Called with `key, value`. It's called *before* |
||||
|
actually removing the item from the internal cache, so if you want |
||||
|
to immediately put it back in, you'll have to do that in a |
||||
|
`nextTick` or `setTimeout` callback or it won't do anything. |
||||
|
* `stale` By default, if you set a `maxAge`, it'll only actually pull |
||||
|
stale items out of the cache when you `get(key)`. (That is, it's |
||||
|
not pre-emptively doing a `setTimeout` or anything.) If you set |
||||
|
`stale:true`, it'll return the stale value before deleting it. If |
||||
|
you don't set this, then it'll return `undefined` when you try to |
||||
|
get a stale entry, as if it had already been deleted. |
||||
|
* `noDisposeOnSet` By default, if you set a `dispose()` method, then |
||||
|
it'll be called whenever a `set()` operation overwrites an existing |
||||
|
key. If you set this option, `dispose()` will only be called when a |
||||
|
key falls out of the cache, not when it is overwritten. |
||||
|
|
||||
|
## API |
||||
|
|
||||
|
* `set(key, value, maxAge)` |
||||
|
* `get(key) => value` |
||||
|
|
||||
|
Both of these will update the "recently used"-ness of the key. |
||||
|
They do what you think. `maxAge` is optional and overrides the |
||||
|
cache `maxAge` option if provided. |
||||
|
|
||||
|
If the key is not found, `get()` will return `undefined`. |
||||
|
|
||||
|
The key and val can be any value. |
||||
|
|
||||
|
* `peek(key)` |
||||
|
|
||||
|
Returns the key value (or `undefined` if not found) without |
||||
|
updating the "recently used"-ness of the key. |
||||
|
|
||||
|
(If you find yourself using this a lot, you *might* be using the |
||||
|
wrong sort of data structure, but there are some use cases where |
||||
|
it's handy.) |
||||
|
|
||||
|
* `del(key)` |
||||
|
|
||||
|
Deletes a key out of the cache. |
||||
|
|
||||
|
* `reset()` |
||||
|
|
||||
|
Clear the cache entirely, throwing away all values. |
||||
|
|
||||
|
* `has(key)` |
||||
|
|
||||
|
Check if a key is in the cache, without updating the recent-ness |
||||
|
or deleting it for being stale. |
||||
|
|
||||
|
* `forEach(function(value,key,cache), [thisp])` |
||||
|
|
||||
|
Just like `Array.prototype.forEach`. Iterates over all the keys |
||||
|
in the cache, in order of recent-ness. (Ie, more recently used |
||||
|
items are iterated over first.) |
||||
|
|
||||
|
* `rforEach(function(value,key,cache), [thisp])` |
||||
|
|
||||
|
The same as `cache.forEach(...)` but items are iterated over in |
||||
|
reverse order. (ie, less recently used items are iterated over |
||||
|
first.) |
||||
|
|
||||
|
* `keys()` |
||||
|
|
||||
|
Return an array of the keys in the cache. |
||||
|
|
||||
|
* `values()` |
||||
|
|
||||
|
Return an array of the values in the cache. |
||||
|
|
||||
|
* `length` |
||||
|
|
||||
|
Return total length of objects in cache taking into account |
||||
|
`length` options function. |
||||
|
|
||||
|
* `itemCount` |
||||
|
|
||||
|
Return total quantity of objects currently in cache. Note, that |
||||
|
`stale` (see options) items are returned as part of this item |
||||
|
count. |
||||
|
|
||||
|
* `dump()` |
||||
|
|
||||
|
Return an array of the cache entries ready for serialization and usage |
||||
|
with 'destinationCache.load(arr)`. |
||||
|
|
||||
|
* `load(cacheEntriesArray)` |
||||
|
|
||||
|
Loads another cache entries array, obtained with `sourceCache.dump()`, |
||||
|
into the cache. The destination cache is reset before loading new entries |
||||
|
|
||||
|
* `prune()` |
||||
|
|
||||
|
Manually iterates over the entire cache proactively pruning old entries |
@ -0,0 +1,467 @@ |
|||||
|
'use strict' |
||||
|
|
||||
|
module.exports = LRUCache |
||||
|
|
||||
|
// This will be a proper iterable 'Map' in engines that support it,
|
||||
|
// or a fakey-fake PseudoMap in older versions.
|
||||
|
var Map = require('pseudomap') |
||||
|
var util = require('util') |
||||
|
|
||||
|
// A linked list to keep track of recently-used-ness
|
||||
|
var Yallist = require('yallist') |
||||
|
|
||||
|
// use symbols if possible, otherwise just _props
|
||||
|
var hasSymbol = typeof Symbol === 'function' |
||||
|
var makeSymbol |
||||
|
if (hasSymbol) { |
||||
|
makeSymbol = function (key) { |
||||
|
return Symbol.for(key) |
||||
|
} |
||||
|
} else { |
||||
|
makeSymbol = function (key) { |
||||
|
return '_' + key |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var MAX = makeSymbol('max') |
||||
|
var LENGTH = makeSymbol('length') |
||||
|
var LENGTH_CALCULATOR = makeSymbol('lengthCalculator') |
||||
|
var ALLOW_STALE = makeSymbol('allowStale') |
||||
|
var MAX_AGE = makeSymbol('maxAge') |
||||
|
var DISPOSE = makeSymbol('dispose') |
||||
|
var NO_DISPOSE_ON_SET = makeSymbol('noDisposeOnSet') |
||||
|
var LRU_LIST = makeSymbol('lruList') |
||||
|
var CACHE = makeSymbol('cache') |
||||
|
|
||||
|
function naiveLength () { return 1 } |
||||
|
|
||||
|
// lruList is a yallist where the head is the youngest
|
||||
|
// item, and the tail is the oldest. the list contains the Hit
|
||||
|
// objects as the entries.
|
||||
|
// Each Hit object has a reference to its Yallist.Node. This
|
||||
|
// never changes.
|
||||
|
//
|
||||
|
// cache is a Map (or PseudoMap) that matches the keys to
|
||||
|
// the Yallist.Node object.
|
||||
|
function LRUCache (options) { |
||||
|
if (!(this instanceof LRUCache)) { |
||||
|
return new LRUCache(options) |
||||
|
} |
||||
|
|
||||
|
if (typeof options === 'number') { |
||||
|
options = { max: options } |
||||
|
} |
||||
|
|
||||
|
if (!options) { |
||||
|
options = {} |
||||
|
} |
||||
|
|
||||
|
var max = this[MAX] = options.max |
||||
|
// Kind of weird to have a default max of Infinity, but oh well.
|
||||
|
if (!max || |
||||
|
!(typeof max === 'number') || |
||||
|
max <= 0) { |
||||
|
this[MAX] = Infinity |
||||
|
} |
||||
|
|
||||
|
var lc = options.length || naiveLength |
||||
|
if (typeof lc !== 'function') { |
||||
|
lc = naiveLength |
||||
|
} |
||||
|
this[LENGTH_CALCULATOR] = lc |
||||
|
|
||||
|
this[ALLOW_STALE] = options.stale || false |
||||
|
this[MAX_AGE] = options.maxAge || 0 |
||||
|
this[DISPOSE] = options.dispose |
||||
|
this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false |
||||
|
this.reset() |
||||
|
} |
||||
|
|
||||
|
// resize the cache when the max changes.
|
||||
|
Object.defineProperty(LRUCache.prototype, 'max', { |
||||
|
set: function (mL) { |
||||
|
if (!mL || !(typeof mL === 'number') || mL <= 0) { |
||||
|
mL = Infinity |
||||
|
} |
||||
|
this[MAX] = mL |
||||
|
trim(this) |
||||
|
}, |
||||
|
get: function () { |
||||
|
return this[MAX] |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
Object.defineProperty(LRUCache.prototype, 'allowStale', { |
||||
|
set: function (allowStale) { |
||||
|
this[ALLOW_STALE] = !!allowStale |
||||
|
}, |
||||
|
get: function () { |
||||
|
return this[ALLOW_STALE] |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
Object.defineProperty(LRUCache.prototype, 'maxAge', { |
||||
|
set: function (mA) { |
||||
|
if (!mA || !(typeof mA === 'number') || mA < 0) { |
||||
|
mA = 0 |
||||
|
} |
||||
|
this[MAX_AGE] = mA |
||||
|
trim(this) |
||||
|
}, |
||||
|
get: function () { |
||||
|
return this[MAX_AGE] |
||||
|
}, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
// resize the cache when the lengthCalculator changes.
|
||||
|
Object.defineProperty(LRUCache.prototype, 'lengthCalculator', { |
||||
|
set: function (lC) { |
||||
|
if (typeof lC !== 'function') { |
||||
|
lC = naiveLength |
||||
|
} |
||||
|
if (lC !== this[LENGTH_CALCULATOR]) { |
||||
|
this[LENGTH_CALCULATOR] = lC |
||||
|
this[LENGTH] = 0 |
||||
|
this[LRU_LIST].forEach(function (hit) { |
||||
|
hit.length = this[LENGTH_CALCULATOR](hit.value, hit.key) |
||||
|
this[LENGTH] += hit.length |
||||
|
}, this) |
||||
|
} |
||||
|
trim(this) |
||||
|
}, |
||||
|
get: function () { return this[LENGTH_CALCULATOR] }, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
Object.defineProperty(LRUCache.prototype, 'length', { |
||||
|
get: function () { return this[LENGTH] }, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
Object.defineProperty(LRUCache.prototype, 'itemCount', { |
||||
|
get: function () { return this[LRU_LIST].length }, |
||||
|
enumerable: true |
||||
|
}) |
||||
|
|
||||
|
LRUCache.prototype.rforEach = function (fn, thisp) { |
||||
|
thisp = thisp || this |
||||
|
for (var walker = this[LRU_LIST].tail; walker !== null;) { |
||||
|
var prev = walker.prev |
||||
|
forEachStep(this, fn, walker, thisp) |
||||
|
walker = prev |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function forEachStep (self, fn, node, thisp) { |
||||
|
var hit = node.value |
||||
|
if (isStale(self, hit)) { |
||||
|
del(self, node) |
||||
|
if (!self[ALLOW_STALE]) { |
||||
|
hit = undefined |
||||
|
} |
||||
|
} |
||||
|
if (hit) { |
||||
|
fn.call(thisp, hit.value, hit.key, self) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.forEach = function (fn, thisp) { |
||||
|
thisp = thisp || this |
||||
|
for (var walker = this[LRU_LIST].head; walker !== null;) { |
||||
|
var next = walker.next |
||||
|
forEachStep(this, fn, walker, thisp) |
||||
|
walker = next |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.keys = function () { |
||||
|
return this[LRU_LIST].toArray().map(function (k) { |
||||
|
return k.key |
||||
|
}, this) |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.values = function () { |
||||
|
return this[LRU_LIST].toArray().map(function (k) { |
||||
|
return k.value |
||||
|
}, this) |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.reset = function () { |
||||
|
if (this[DISPOSE] && |
||||
|
this[LRU_LIST] && |
||||
|
this[LRU_LIST].length) { |
||||
|
this[LRU_LIST].forEach(function (hit) { |
||||
|
this[DISPOSE](hit.key, hit.value) |
||||
|
}, this) |
||||
|
} |
||||
|
|
||||
|
this[CACHE] = new Map() // hash of items by key
|
||||
|
this[LRU_LIST] = new Yallist() // list of items in order of use recency
|
||||
|
this[LENGTH] = 0 // length of items in the list
|
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.dump = function () { |
||||
|
return this[LRU_LIST].map(function (hit) { |
||||
|
if (!isStale(this, hit)) { |
||||
|
return { |
||||
|
k: hit.key, |
||||
|
v: hit.value, |
||||
|
e: hit.now + (hit.maxAge || 0) |
||||
|
} |
||||
|
} |
||||
|
}, this).toArray().filter(function (h) { |
||||
|
return h |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.dumpLru = function () { |
||||
|
return this[LRU_LIST] |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.inspect = function (n, opts) { |
||||
|
var str = 'LRUCache {' |
||||
|
var extras = false |
||||
|
|
||||
|
var as = this[ALLOW_STALE] |
||||
|
if (as) { |
||||
|
str += '\n allowStale: true' |
||||
|
extras = true |
||||
|
} |
||||
|
|
||||
|
var max = this[MAX] |
||||
|
if (max && max !== Infinity) { |
||||
|
if (extras) { |
||||
|
str += ',' |
||||
|
} |
||||
|
str += '\n max: ' + util.inspect(max, opts) |
||||
|
extras = true |
||||
|
} |
||||
|
|
||||
|
var maxAge = this[MAX_AGE] |
||||
|
if (maxAge) { |
||||
|
if (extras) { |
||||
|
str += ',' |
||||
|
} |
||||
|
str += '\n maxAge: ' + util.inspect(maxAge, opts) |
||||
|
extras = true |
||||
|
} |
||||
|
|
||||
|
var lc = this[LENGTH_CALCULATOR] |
||||
|
if (lc && lc !== naiveLength) { |
||||
|
if (extras) { |
||||
|
str += ',' |
||||
|
} |
||||
|
str += '\n length: ' + util.inspect(this[LENGTH], opts) |
||||
|
extras = true |
||||
|
} |
||||
|
|
||||
|
var didFirst = false |
||||
|
this[LRU_LIST].forEach(function (item) { |
||||
|
if (didFirst) { |
||||
|
str += ',\n ' |
||||
|
} else { |
||||
|
if (extras) { |
||||
|
str += ',\n' |
||||
|
} |
||||
|
didFirst = true |
||||
|
str += '\n ' |
||||
|
} |
||||
|
var key = util.inspect(item.key).split('\n').join('\n ') |
||||
|
var val = { value: item.value } |
||||
|
if (item.maxAge !== maxAge) { |
||||
|
val.maxAge = item.maxAge |
||||
|
} |
||||
|
if (lc !== naiveLength) { |
||||
|
val.length = item.length |
||||
|
} |
||||
|
if (isStale(this, item)) { |
||||
|
val.stale = true |
||||
|
} |
||||
|
|
||||
|
val = util.inspect(val, opts).split('\n').join('\n ') |
||||
|
str += key + ' => ' + val |
||||
|
}) |
||||
|
|
||||
|
if (didFirst || extras) { |
||||
|
str += '\n' |
||||
|
} |
||||
|
str += '}' |
||||
|
|
||||
|
return str |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.set = function (key, value, maxAge) { |
||||
|
maxAge = maxAge || this[MAX_AGE] |
||||
|
|
||||
|
var now = maxAge ? Date.now() : 0 |
||||
|
var len = this[LENGTH_CALCULATOR](value, key) |
||||
|
|
||||
|
if (this[CACHE].has(key)) { |
||||
|
if (len > this[MAX]) { |
||||
|
del(this, this[CACHE].get(key)) |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
var node = this[CACHE].get(key) |
||||
|
var item = node.value |
||||
|
|
||||
|
// dispose of the old one before overwriting
|
||||
|
// split out into 2 ifs for better coverage tracking
|
||||
|
if (this[DISPOSE]) { |
||||
|
if (!this[NO_DISPOSE_ON_SET]) { |
||||
|
this[DISPOSE](key, item.value) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
item.now = now |
||||
|
item.maxAge = maxAge |
||||
|
item.value = value |
||||
|
this[LENGTH] += len - item.length |
||||
|
item.length = len |
||||
|
this.get(key) |
||||
|
trim(this) |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
var hit = new Entry(key, value, len, now, maxAge) |
||||
|
|
||||
|
// oversized objects fall out of cache automatically.
|
||||
|
if (hit.length > this[MAX]) { |
||||
|
if (this[DISPOSE]) { |
||||
|
this[DISPOSE](key, value) |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
this[LENGTH] += hit.length |
||||
|
this[LRU_LIST].unshift(hit) |
||||
|
this[CACHE].set(key, this[LRU_LIST].head) |
||||
|
trim(this) |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.has = function (key) { |
||||
|
if (!this[CACHE].has(key)) return false |
||||
|
var hit = this[CACHE].get(key).value |
||||
|
if (isStale(this, hit)) { |
||||
|
return false |
||||
|
} |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.get = function (key) { |
||||
|
return get(this, key, true) |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.peek = function (key) { |
||||
|
return get(this, key, false) |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.pop = function () { |
||||
|
var node = this[LRU_LIST].tail |
||||
|
if (!node) return null |
||||
|
del(this, node) |
||||
|
return node.value |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.del = function (key) { |
||||
|
del(this, this[CACHE].get(key)) |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.load = function (arr) { |
||||
|
// reset the cache
|
||||
|
this.reset() |
||||
|
|
||||
|
var now = Date.now() |
||||
|
// A previous serialized cache has the most recent items first
|
||||
|
for (var l = arr.length - 1; l >= 0; l--) { |
||||
|
var hit = arr[l] |
||||
|
var expiresAt = hit.e || 0 |
||||
|
if (expiresAt === 0) { |
||||
|
// the item was created without expiration in a non aged cache
|
||||
|
this.set(hit.k, hit.v) |
||||
|
} else { |
||||
|
var maxAge = expiresAt - now |
||||
|
// dont add already expired items
|
||||
|
if (maxAge > 0) { |
||||
|
this.set(hit.k, hit.v, maxAge) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
LRUCache.prototype.prune = function () { |
||||
|
var self = this |
||||
|
this[CACHE].forEach(function (value, key) { |
||||
|
get(self, key, false) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
function get (self, key, doUse) { |
||||
|
var node = self[CACHE].get(key) |
||||
|
if (node) { |
||||
|
var hit = node.value |
||||
|
if (isStale(self, hit)) { |
||||
|
del(self, node) |
||||
|
if (!self[ALLOW_STALE]) hit = undefined |
||||
|
} else { |
||||
|
if (doUse) { |
||||
|
self[LRU_LIST].unshiftNode(node) |
||||
|
} |
||||
|
} |
||||
|
if (hit) hit = hit.value |
||||
|
} |
||||
|
return hit |
||||
|
} |
||||
|
|
||||
|
function isStale (self, hit) { |
||||
|
if (!hit || (!hit.maxAge && !self[MAX_AGE])) { |
||||
|
return false |
||||
|
} |
||||
|
var stale = false |
||||
|
var diff = Date.now() - hit.now |
||||
|
if (hit.maxAge) { |
||||
|
stale = diff > hit.maxAge |
||||
|
} else { |
||||
|
stale = self[MAX_AGE] && (diff > self[MAX_AGE]) |
||||
|
} |
||||
|
return stale |
||||
|
} |
||||
|
|
||||
|
function trim (self) { |
||||
|
if (self[LENGTH] > self[MAX]) { |
||||
|
for (var walker = self[LRU_LIST].tail; |
||||
|
self[LENGTH] > self[MAX] && walker !== null;) { |
||||
|
// We know that we're about to delete this one, and also
|
||||
|
// what the next least recently used key will be, so just
|
||||
|
// go ahead and set it now.
|
||||
|
var prev = walker.prev |
||||
|
del(self, walker) |
||||
|
walker = prev |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function del (self, node) { |
||||
|
if (node) { |
||||
|
var hit = node.value |
||||
|
if (self[DISPOSE]) { |
||||
|
self[DISPOSE](hit.key, hit.value) |
||||
|
} |
||||
|
self[LENGTH] -= hit.length |
||||
|
self[CACHE].delete(hit.key) |
||||
|
self[LRU_LIST].removeNode(node) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// classy, since V8 prefers predictable objects.
|
||||
|
function Entry (key, value, length, now, maxAge) { |
||||
|
this.key = key |
||||
|
this.value = value |
||||
|
this.length = length |
||||
|
this.now = now |
||||
|
this.maxAge = maxAge || 0 |
||||
|
} |
@ -0,0 +1,68 @@ |
|||||
|
{ |
||||
|
"_from": "lru-cache@^4.0.1", |
||||
|
"_id": "lru-cache@4.1.1", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha512-q4spe4KTfsAS1SUHLO0wz8Qiyf1+vMIAgpRYioFYDMNqKfHQbg+AVDH3i4fvpl71/P1L0dBl+fQi+P37UYf0ew==", |
||||
|
"_location": "/eslint/lru-cache", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "lru-cache@^4.0.1", |
||||
|
"name": "lru-cache", |
||||
|
"escapedName": "lru-cache", |
||||
|
"rawSpec": "^4.0.1", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^4.0.1" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint/cross-spawn" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.1.tgz", |
||||
|
"_shasum": "622e32e82488b49279114a4f9ecf45e7cd6bba55", |
||||
|
"_spec": "lru-cache@^4.0.1", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint/node_modules/cross-spawn", |
||||
|
"author": { |
||||
|
"name": "Isaac Z. Schlueter", |
||||
|
"email": "i@izs.me" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/isaacs/node-lru-cache/issues" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"dependencies": { |
||||
|
"pseudomap": "^1.0.2", |
||||
|
"yallist": "^2.1.2" |
||||
|
}, |
||||
|
"deprecated": false, |
||||
|
"description": "A cache object that deletes the least-recently-used items.", |
||||
|
"devDependencies": { |
||||
|
"benchmark": "^2.1.4", |
||||
|
"standard": "^5.4.1", |
||||
|
"tap": "^10.3.3" |
||||
|
}, |
||||
|
"files": [ |
||||
|
"index.js" |
||||
|
], |
||||
|
"homepage": "https://github.com/isaacs/node-lru-cache#readme", |
||||
|
"keywords": [ |
||||
|
"mru", |
||||
|
"lru", |
||||
|
"cache" |
||||
|
], |
||||
|
"license": "ISC", |
||||
|
"main": "index.js", |
||||
|
"name": "lru-cache", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git://github.com/isaacs/node-lru-cache.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"postpublish": "git push origin --all; git push origin --tags", |
||||
|
"posttest": "standard test/*.js index.js", |
||||
|
"postversion": "npm publish", |
||||
|
"preversion": "npm test", |
||||
|
"test": "tap test/*.js --100 -J" |
||||
|
}, |
||||
|
"version": "4.1.1" |
||||
|
} |
@ -1,6 +0,0 @@ |
|||||
<!--remark setext--> |
|
||||
|
|
||||
<!--lint disable no-multiple-toplevel-headings --> |
|
||||
|
|
||||
1.0.0 / 2016-07-16 |
|
||||
================== |
|
@ -0,0 +1,15 @@ |
|||||
|
The ISC License |
||||
|
|
||||
|
Copyright (c) Isaac Z. Schlueter and Contributors |
||||
|
|
||||
|
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. |
@ -0,0 +1,60 @@ |
|||||
|
# pseudomap |
||||
|
|
||||
|
A thing that is a lot like ES6 `Map`, but without iterators, for use |
||||
|
in environments where `for..of` syntax and `Map` are not available. |
||||
|
|
||||
|
If you need iterators, or just in general a more faithful polyfill to |
||||
|
ES6 Maps, check out [es6-map](http://npm.im/es6-map). |
||||
|
|
||||
|
If you are in an environment where `Map` is supported, then that will |
||||
|
be returned instead, unless `process.env.TEST_PSEUDOMAP` is set. |
||||
|
|
||||
|
You can use any value as keys, and any value as data. Setting again |
||||
|
with the identical key will overwrite the previous value. |
||||
|
|
||||
|
Internally, data is stored on an `Object.create(null)` style object. |
||||
|
The key is coerced to a string to generate the key on the internal |
||||
|
data-bag object. The original key used is stored along with the data. |
||||
|
|
||||
|
In the event of a stringified-key collision, a new key is generated by |
||||
|
appending an increasing number to the stringified-key until finding |
||||
|
either the intended key or an empty spot. |
||||
|
|
||||
|
Note that because object traversal order of plain objects is not |
||||
|
guaranteed to be identical to insertion order, the insertion order |
||||
|
guarantee of `Map.prototype.forEach` is not guaranteed in this |
||||
|
implementation. However, in all versions of Node.js and V8 where this |
||||
|
module works, `forEach` does traverse data in insertion order. |
||||
|
|
||||
|
## API |
||||
|
|
||||
|
Most of the [Map |
||||
|
API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), |
||||
|
with the following exceptions: |
||||
|
|
||||
|
1. A `Map` object is not an iterator. |
||||
|
2. `values`, `keys`, and `entries` methods are not implemented, |
||||
|
because they return iterators. |
||||
|
3. The argument to the constructor can be an Array of `[key, value]` |
||||
|
pairs, or a `Map` or `PseudoMap` object. But, since iterators |
||||
|
aren't used, passing any plain-old iterator won't initialize the |
||||
|
map properly. |
||||
|
|
||||
|
## USAGE |
||||
|
|
||||
|
Use just like a regular ES6 Map. |
||||
|
|
||||
|
```javascript |
||||
|
var PseudoMap = require('pseudomap') |
||||
|
|
||||
|
// optionally provide a pseudomap, or an array of [key,value] pairs |
||||
|
// as the argument to initialize the map with |
||||
|
var myMap = new PseudoMap() |
||||
|
|
||||
|
myMap.set(1, 'number 1') |
||||
|
myMap.set('1', 'string 1') |
||||
|
var akey = {} |
||||
|
var bkey = {} |
||||
|
myMap.set(akey, { some: 'data' }) |
||||
|
myMap.set(bkey, { some: 'other data' }) |
||||
|
``` |
@ -0,0 +1,9 @@ |
|||||
|
if (process.env.npm_package_name === 'pseudomap' && |
||||
|
process.env.npm_lifecycle_script === 'test') |
||||
|
process.env.TEST_PSEUDOMAP = 'true' |
||||
|
|
||||
|
if (typeof Map === 'function' && !process.env.TEST_PSEUDOMAP) { |
||||
|
module.exports = Map |
||||
|
} else { |
||||
|
module.exports = require('./pseudomap') |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
{ |
||||
|
"_from": "pseudomap@^1.0.2", |
||||
|
"_id": "pseudomap@1.0.2", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", |
||||
|
"_location": "/eslint/pseudomap", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "pseudomap@^1.0.2", |
||||
|
"name": "pseudomap", |
||||
|
"escapedName": "pseudomap", |
||||
|
"rawSpec": "^1.0.2", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^1.0.2" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint/lru-cache" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", |
||||
|
"_shasum": "f052a28da70e618917ef0a8ac34c1ae5a68286b3", |
||||
|
"_spec": "pseudomap@^1.0.2", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint/node_modules/lru-cache", |
||||
|
"author": { |
||||
|
"name": "Isaac Z. Schlueter", |
||||
|
"email": "i@izs.me", |
||||
|
"url": "http://blog.izs.me/" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/isaacs/pseudomap/issues" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"deprecated": false, |
||||
|
"description": "A thing that is a lot like ES6 `Map`, but without iterators, for use in environments where `for..of` syntax and `Map` are not available.", |
||||
|
"devDependencies": { |
||||
|
"tap": "^2.3.1" |
||||
|
}, |
||||
|
"directories": { |
||||
|
"test": "test" |
||||
|
}, |
||||
|
"homepage": "https://github.com/isaacs/pseudomap#readme", |
||||
|
"license": "ISC", |
||||
|
"main": "map.js", |
||||
|
"name": "pseudomap", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git+https://github.com/isaacs/pseudomap.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"test": "tap test/*.js" |
||||
|
}, |
||||
|
"version": "1.0.2" |
||||
|
} |
@ -0,0 +1,113 @@ |
|||||
|
var hasOwnProperty = Object.prototype.hasOwnProperty |
||||
|
|
||||
|
module.exports = PseudoMap |
||||
|
|
||||
|
function PseudoMap (set) { |
||||
|
if (!(this instanceof PseudoMap)) // whyyyyyyy
|
||||
|
throw new TypeError("Constructor PseudoMap requires 'new'") |
||||
|
|
||||
|
this.clear() |
||||
|
|
||||
|
if (set) { |
||||
|
if ((set instanceof PseudoMap) || |
||||
|
(typeof Map === 'function' && set instanceof Map)) |
||||
|
set.forEach(function (value, key) { |
||||
|
this.set(key, value) |
||||
|
}, this) |
||||
|
else if (Array.isArray(set)) |
||||
|
set.forEach(function (kv) { |
||||
|
this.set(kv[0], kv[1]) |
||||
|
}, this) |
||||
|
else |
||||
|
throw new TypeError('invalid argument') |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PseudoMap.prototype.forEach = function (fn, thisp) { |
||||
|
thisp = thisp || this |
||||
|
Object.keys(this._data).forEach(function (k) { |
||||
|
if (k !== 'size') |
||||
|
fn.call(thisp, this._data[k].value, this._data[k].key) |
||||
|
}, this) |
||||
|
} |
||||
|
|
||||
|
PseudoMap.prototype.has = function (k) { |
||||
|
return !!find(this._data, k) |
||||
|
} |
||||
|
|
||||
|
PseudoMap.prototype.get = function (k) { |
||||
|
var res = find(this._data, k) |
||||
|
return res && res.value |
||||
|
} |
||||
|
|
||||
|
PseudoMap.prototype.set = function (k, v) { |
||||
|
set(this._data, k, v) |
||||
|
} |
||||
|
|
||||
|
PseudoMap.prototype.delete = function (k) { |
||||
|
var res = find(this._data, k) |
||||
|
if (res) { |
||||
|
delete this._data[res._index] |
||||
|
this._data.size-- |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PseudoMap.prototype.clear = function () { |
||||
|
var data = Object.create(null) |
||||
|
data.size = 0 |
||||
|
|
||||
|
Object.defineProperty(this, '_data', { |
||||
|
value: data, |
||||
|
enumerable: false, |
||||
|
configurable: true, |
||||
|
writable: false |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
Object.defineProperty(PseudoMap.prototype, 'size', { |
||||
|
get: function () { |
||||
|
return this._data.size |
||||
|
}, |
||||
|
set: function (n) {}, |
||||
|
enumerable: true, |
||||
|
configurable: true |
||||
|
}) |
||||
|
|
||||
|
PseudoMap.prototype.values = |
||||
|
PseudoMap.prototype.keys = |
||||
|
PseudoMap.prototype.entries = function () { |
||||
|
throw new Error('iterators are not implemented in this version') |
||||
|
} |
||||
|
|
||||
|
// Either identical, or both NaN
|
||||
|
function same (a, b) { |
||||
|
return a === b || a !== a && b !== b |
||||
|
} |
||||
|
|
||||
|
function Entry (k, v, i) { |
||||
|
this.key = k |
||||
|
this.value = v |
||||
|
this._index = i |
||||
|
} |
||||
|
|
||||
|
function find (data, k) { |
||||
|
for (var i = 0, s = '_' + k, key = s; |
||||
|
hasOwnProperty.call(data, key); |
||||
|
key = s + i++) { |
||||
|
if (same(data[key].key, k)) |
||||
|
return data[key] |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function set (data, k, v) { |
||||
|
for (var i = 0, s = '_' + k, key = s; |
||||
|
hasOwnProperty.call(data, key); |
||||
|
key = s + i++) { |
||||
|
if (same(data[key].key, k)) { |
||||
|
data[key].value = v |
||||
|
return |
||||
|
} |
||||
|
} |
||||
|
data.size++ |
||||
|
data[key] = new Entry(k, v, key) |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
The ISC License |
||||
|
|
||||
|
Copyright (c) Isaac Z. Schlueter and Contributors |
||||
|
|
||||
|
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. |
@ -0,0 +1,350 @@ |
|||||
|
semver(1) -- The semantic versioner for npm |
||||
|
=========================================== |
||||
|
|
||||
|
## Usage |
||||
|
|
||||
|
$ npm install semver |
||||
|
$ node |
||||
|
var semver = require('semver') |
||||
|
|
||||
|
semver.valid('1.2.3') // '1.2.3' |
||||
|
semver.valid('a.b.c') // null |
||||
|
semver.clean(' =v1.2.3 ') // '1.2.3' |
||||
|
semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true |
||||
|
semver.gt('1.2.3', '9.8.7') // false |
||||
|
semver.lt('1.2.3', '9.8.7') // true |
||||
|
|
||||
|
As a command-line utility: |
||||
|
|
||||
|
$ semver -h |
||||
|
|
||||
|
SemVer 5.1.0 |
||||
|
|
||||
|
A JavaScript implementation of the http://semver.org/ specification |
||||
|
Copyright Isaac Z. Schlueter |
||||
|
|
||||
|
Usage: semver [options] <version> [<version> [...]] |
||||
|
Prints valid versions sorted by SemVer precedence |
||||
|
|
||||
|
Options: |
||||
|
-r --range <range> |
||||
|
Print versions that match the specified range. |
||||
|
|
||||
|
-i --increment [<level>] |
||||
|
Increment a version by the specified level. Level can |
||||
|
be one of: major, minor, patch, premajor, preminor, |
||||
|
prepatch, or prerelease. Default level is 'patch'. |
||||
|
Only one version may be specified. |
||||
|
|
||||
|
--preid <identifier> |
||||
|
Identifier to be used to prefix premajor, preminor, |
||||
|
prepatch or prerelease version increments. |
||||
|
|
||||
|
-l --loose |
||||
|
Interpret versions and ranges loosely |
||||
|
|
||||
|
Program exits successfully if any valid version satisfies |
||||
|
all supplied ranges, and prints all satisfying versions. |
||||
|
|
||||
|
If no satisfying versions are found, then exits failure. |
||||
|
|
||||
|
Versions are printed in ascending order, so supplying |
||||
|
multiple versions to the utility will just sort them. |
||||
|
|
||||
|
## Versions |
||||
|
|
||||
|
A "version" is described by the `v2.0.0` specification found at |
||||
|
<http://semver.org/>. |
||||
|
|
||||
|
A leading `"="` or `"v"` character is stripped off and ignored. |
||||
|
|
||||
|
## Ranges |
||||
|
|
||||
|
A `version range` is a set of `comparators` which specify versions |
||||
|
that satisfy the range. |
||||
|
|
||||
|
A `comparator` is composed of an `operator` and a `version`. The set |
||||
|
of primitive `operators` is: |
||||
|
|
||||
|
* `<` Less than |
||||
|
* `<=` Less than or equal to |
||||
|
* `>` Greater than |
||||
|
* `>=` Greater than or equal to |
||||
|
* `=` Equal. If no operator is specified, then equality is assumed, |
||||
|
so this operator is optional, but MAY be included. |
||||
|
|
||||
|
For example, the comparator `>=1.2.7` would match the versions |
||||
|
`1.2.7`, `1.2.8`, `2.5.3`, and `1.3.9`, but not the versions `1.2.6` |
||||
|
or `1.1.0`. |
||||
|
|
||||
|
Comparators can be joined by whitespace to form a `comparator set`, |
||||
|
which is satisfied by the **intersection** of all of the comparators |
||||
|
it includes. |
||||
|
|
||||
|
A range is composed of one or more comparator sets, joined by `||`. A |
||||
|
version matches a range if and only if every comparator in at least |
||||
|
one of the `||`-separated comparator sets is satisfied by the version. |
||||
|
|
||||
|
For example, the range `>=1.2.7 <1.3.0` would match the versions |
||||
|
`1.2.7`, `1.2.8`, and `1.2.99`, but not the versions `1.2.6`, `1.3.0`, |
||||
|
or `1.1.0`. |
||||
|
|
||||
|
The range `1.2.7 || >=1.2.9 <2.0.0` would match the versions `1.2.7`, |
||||
|
`1.2.9`, and `1.4.6`, but not the versions `1.2.8` or `2.0.0`. |
||||
|
|
||||
|
### Prerelease Tags |
||||
|
|
||||
|
If a version has a prerelease tag (for example, `1.2.3-alpha.3`) then |
||||
|
it will only be allowed to satisfy comparator sets if at least one |
||||
|
comparator with the same `[major, minor, patch]` tuple also has a |
||||
|
prerelease tag. |
||||
|
|
||||
|
For example, the range `>1.2.3-alpha.3` would be allowed to match the |
||||
|
version `1.2.3-alpha.7`, but it would *not* be satisfied by |
||||
|
`3.4.5-alpha.9`, even though `3.4.5-alpha.9` is technically "greater |
||||
|
than" `1.2.3-alpha.3` according to the SemVer sort rules. The version |
||||
|
range only accepts prerelease tags on the `1.2.3` version. The |
||||
|
version `3.4.5` *would* satisfy the range, because it does not have a |
||||
|
prerelease flag, and `3.4.5` is greater than `1.2.3-alpha.7`. |
||||
|
|
||||
|
The purpose for this behavior is twofold. First, prerelease versions |
||||
|
frequently are updated very quickly, and contain many breaking changes |
||||
|
that are (by the author's design) not yet fit for public consumption. |
||||
|
Therefore, by default, they are excluded from range matching |
||||
|
semantics. |
||||
|
|
||||
|
Second, a user who has opted into using a prerelease version has |
||||
|
clearly indicated the intent to use *that specific* set of |
||||
|
alpha/beta/rc versions. By including a prerelease tag in the range, |
||||
|
the user is indicating that they are aware of the risk. However, it |
||||
|
is still not appropriate to assume that they have opted into taking a |
||||
|
similar risk on the *next* set of prerelease versions. |
||||
|
|
||||
|
#### Prerelease Identifiers |
||||
|
|
||||
|
The method `.inc` takes an additional `identifier` string argument that |
||||
|
will append the value of the string as a prerelease identifier: |
||||
|
|
||||
|
```javascript |
||||
|
> semver.inc('1.2.3', 'prerelease', 'beta') |
||||
|
'1.2.4-beta.0' |
||||
|
``` |
||||
|
|
||||
|
command-line example: |
||||
|
|
||||
|
```shell |
||||
|
$ semver 1.2.3 -i prerelease --preid beta |
||||
|
1.2.4-beta.0 |
||||
|
``` |
||||
|
|
||||
|
Which then can be used to increment further: |
||||
|
|
||||
|
```shell |
||||
|
$ semver 1.2.4-beta.0 -i prerelease |
||||
|
1.2.4-beta.1 |
||||
|
``` |
||||
|
|
||||
|
### Advanced Range Syntax |
||||
|
|
||||
|
Advanced range syntax desugars to primitive comparators in |
||||
|
deterministic ways. |
||||
|
|
||||
|
Advanced ranges may be combined in the same way as primitive |
||||
|
comparators using white space or `||`. |
||||
|
|
||||
|
#### Hyphen Ranges `X.Y.Z - A.B.C` |
||||
|
|
||||
|
Specifies an inclusive set. |
||||
|
|
||||
|
* `1.2.3 - 2.3.4` := `>=1.2.3 <=2.3.4` |
||||
|
|
||||
|
If a partial version is provided as the first version in the inclusive |
||||
|
range, then the missing pieces are replaced with zeroes. |
||||
|
|
||||
|
* `1.2 - 2.3.4` := `>=1.2.0 <=2.3.4` |
||||
|
|
||||
|
If a partial version is provided as the second version in the |
||||
|
inclusive range, then all versions that start with the supplied parts |
||||
|
of the tuple are accepted, but nothing that would be greater than the |
||||
|
provided tuple parts. |
||||
|
|
||||
|
* `1.2.3 - 2.3` := `>=1.2.3 <2.4.0` |
||||
|
* `1.2.3 - 2` := `>=1.2.3 <3.0.0` |
||||
|
|
||||
|
#### X-Ranges `1.2.x` `1.X` `1.2.*` `*` |
||||
|
|
||||
|
Any of `X`, `x`, or `*` may be used to "stand in" for one of the |
||||
|
numeric values in the `[major, minor, patch]` tuple. |
||||
|
|
||||
|
* `*` := `>=0.0.0` (Any version satisfies) |
||||
|
* `1.x` := `>=1.0.0 <2.0.0` (Matching major version) |
||||
|
* `1.2.x` := `>=1.2.0 <1.3.0` (Matching major and minor versions) |
||||
|
|
||||
|
A partial version range is treated as an X-Range, so the special |
||||
|
character is in fact optional. |
||||
|
|
||||
|
* `""` (empty string) := `*` := `>=0.0.0` |
||||
|
* `1` := `1.x.x` := `>=1.0.0 <2.0.0` |
||||
|
* `1.2` := `1.2.x` := `>=1.2.0 <1.3.0` |
||||
|
|
||||
|
#### Tilde Ranges `~1.2.3` `~1.2` `~1` |
||||
|
|
||||
|
Allows patch-level changes if a minor version is specified on the |
||||
|
comparator. Allows minor-level changes if not. |
||||
|
|
||||
|
* `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0` |
||||
|
* `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0` (Same as `1.2.x`) |
||||
|
* `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0` (Same as `1.x`) |
||||
|
* `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0` |
||||
|
* `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0` (Same as `0.2.x`) |
||||
|
* `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0` (Same as `0.x`) |
||||
|
* `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0` Note that prereleases in |
||||
|
the `1.2.3` version will be allowed, if they are greater than or |
||||
|
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but |
||||
|
`1.2.4-beta.2` would not, because it is a prerelease of a |
||||
|
different `[major, minor, patch]` tuple. |
||||
|
|
||||
|
#### Caret Ranges `^1.2.3` `^0.2.5` `^0.0.4` |
||||
|
|
||||
|
Allows changes that do not modify the left-most non-zero digit in the |
||||
|
`[major, minor, patch]` tuple. In other words, this allows patch and |
||||
|
minor updates for versions `1.0.0` and above, patch updates for |
||||
|
versions `0.X >=0.1.0`, and *no* updates for versions `0.0.X`. |
||||
|
|
||||
|
Many authors treat a `0.x` version as if the `x` were the major |
||||
|
"breaking-change" indicator. |
||||
|
|
||||
|
Caret ranges are ideal when an author may make breaking changes |
||||
|
between `0.2.4` and `0.3.0` releases, which is a common practice. |
||||
|
However, it presumes that there will *not* be breaking changes between |
||||
|
`0.2.4` and `0.2.5`. It allows for changes that are presumed to be |
||||
|
additive (but non-breaking), according to commonly observed practices. |
||||
|
|
||||
|
* `^1.2.3` := `>=1.2.3 <2.0.0` |
||||
|
* `^0.2.3` := `>=0.2.3 <0.3.0` |
||||
|
* `^0.0.3` := `>=0.0.3 <0.0.4` |
||||
|
* `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0` Note that prereleases in |
||||
|
the `1.2.3` version will be allowed, if they are greater than or |
||||
|
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but |
||||
|
`1.2.4-beta.2` would not, because it is a prerelease of a |
||||
|
different `[major, minor, patch]` tuple. |
||||
|
* `^0.0.3-beta` := `>=0.0.3-beta <0.0.4` Note that prereleases in the |
||||
|
`0.0.3` version *only* will be allowed, if they are greater than or |
||||
|
equal to `beta`. So, `0.0.3-pr.2` would be allowed. |
||||
|
|
||||
|
When parsing caret ranges, a missing `patch` value desugars to the |
||||
|
number `0`, but will allow flexibility within that value, even if the |
||||
|
major and minor versions are both `0`. |
||||
|
|
||||
|
* `^1.2.x` := `>=1.2.0 <2.0.0` |
||||
|
* `^0.0.x` := `>=0.0.0 <0.1.0` |
||||
|
* `^0.0` := `>=0.0.0 <0.1.0` |
||||
|
|
||||
|
A missing `minor` and `patch` values will desugar to zero, but also |
||||
|
allow flexibility within those values, even if the major version is |
||||
|
zero. |
||||
|
|
||||
|
* `^1.x` := `>=1.0.0 <2.0.0` |
||||
|
* `^0.x` := `>=0.0.0 <1.0.0` |
||||
|
|
||||
|
### Range Grammar |
||||
|
|
||||
|
Putting all this together, here is a Backus-Naur grammar for ranges, |
||||
|
for the benefit of parser authors: |
||||
|
|
||||
|
```bnf |
||||
|
range-set ::= range ( logical-or range ) * |
||||
|
logical-or ::= ( ' ' ) * '||' ( ' ' ) * |
||||
|
range ::= hyphen | simple ( ' ' simple ) * | '' |
||||
|
hyphen ::= partial ' - ' partial |
||||
|
simple ::= primitive | partial | tilde | caret |
||||
|
primitive ::= ( '<' | '>' | '>=' | '<=' | '=' | ) partial |
||||
|
partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )? |
||||
|
xr ::= 'x' | 'X' | '*' | nr |
||||
|
nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) * |
||||
|
tilde ::= '~' partial |
||||
|
caret ::= '^' partial |
||||
|
qualifier ::= ( '-' pre )? ( '+' build )? |
||||
|
pre ::= parts |
||||
|
build ::= parts |
||||
|
parts ::= part ( '.' part ) * |
||||
|
part ::= nr | [-0-9A-Za-z]+ |
||||
|
``` |
||||
|
|
||||
|
## Functions |
||||
|
|
||||
|
All methods and classes take a final `loose` boolean argument that, if |
||||
|
true, will be more forgiving about not-quite-valid semver strings. |
||||
|
The resulting output will always be 100% strict, of course. |
||||
|
|
||||
|
Strict-mode Comparators and Ranges will be strict about the SemVer |
||||
|
strings that they parse. |
||||
|
|
||||
|
* `valid(v)`: Return the parsed version, or null if it's not valid. |
||||
|
* `inc(v, release)`: Return the version incremented by the release |
||||
|
type (`major`, `premajor`, `minor`, `preminor`, `patch`, |
||||
|
`prepatch`, or `prerelease`), or null if it's not valid |
||||
|
* `premajor` in one call will bump the version up to the next major |
||||
|
version and down to a prerelease of that major version. |
||||
|
`preminor`, and `prepatch` work the same way. |
||||
|
* If called from a non-prerelease version, the `prerelease` will work the |
||||
|
same as `prepatch`. It increments the patch version, then makes a |
||||
|
prerelease. If the input version is already a prerelease it simply |
||||
|
increments it. |
||||
|
* `prerelease(v)`: Returns an array of prerelease components, or null |
||||
|
if none exist. Example: `prerelease('1.2.3-alpha.1') -> ['alpha', 1]` |
||||
|
* `major(v)`: Return the major version number. |
||||
|
* `minor(v)`: Return the minor version number. |
||||
|
* `patch(v)`: Return the patch version number. |
||||
|
|
||||
|
### Comparison |
||||
|
|
||||
|
* `gt(v1, v2)`: `v1 > v2` |
||||
|
* `gte(v1, v2)`: `v1 >= v2` |
||||
|
* `lt(v1, v2)`: `v1 < v2` |
||||
|
* `lte(v1, v2)`: `v1 <= v2` |
||||
|
* `eq(v1, v2)`: `v1 == v2` This is true if they're logically equivalent, |
||||
|
even if they're not the exact same string. You already know how to |
||||
|
compare strings. |
||||
|
* `neq(v1, v2)`: `v1 != v2` The opposite of `eq`. |
||||
|
* `cmp(v1, comparator, v2)`: Pass in a comparison string, and it'll call |
||||
|
the corresponding function above. `"==="` and `"!=="` do simple |
||||
|
string comparison, but are included for completeness. Throws if an |
||||
|
invalid comparison string is provided. |
||||
|
* `compare(v1, v2)`: Return `0` if `v1 == v2`, or `1` if `v1` is greater, or `-1` if |
||||
|
`v2` is greater. Sorts in ascending order if passed to `Array.sort()`. |
||||
|
* `rcompare(v1, v2)`: The reverse of compare. Sorts an array of versions |
||||
|
in descending order when passed to `Array.sort()`. |
||||
|
* `diff(v1, v2)`: Returns difference between two versions by the release type |
||||
|
(`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or `prerelease`), |
||||
|
or null if the versions are the same. |
||||
|
|
||||
|
|
||||
|
### Ranges |
||||
|
|
||||
|
* `validRange(range)`: Return the valid range or null if it's not valid |
||||
|
* `satisfies(version, range)`: Return true if the version satisfies the |
||||
|
range. |
||||
|
* `maxSatisfying(versions, range)`: Return the highest version in the list |
||||
|
that satisfies the range, or `null` if none of them do. |
||||
|
* `minSatisfying(versions, range)`: Return the lowest version in the list |
||||
|
that satisfies the range, or `null` if none of them do. |
||||
|
* `gtr(version, range)`: Return `true` if version is greater than all the |
||||
|
versions possible in the range. |
||||
|
* `ltr(version, range)`: Return `true` if version is less than all the |
||||
|
versions possible in the range. |
||||
|
* `outside(version, range, hilo)`: Return true if the version is outside |
||||
|
the bounds of the range in either the high or low direction. The |
||||
|
`hilo` argument must be either the string `'>'` or `'<'`. (This is |
||||
|
the function called by `gtr` and `ltr`.) |
||||
|
|
||||
|
Note that, since ranges may be non-contiguous, a version might not be |
||||
|
greater than a range, less than a range, *or* satisfy a range! For |
||||
|
example, the range `1.2 <1.2.9 || >2.0.0` would have a hole from `1.2.9` |
||||
|
until `2.0.0`, so the version `1.2.10` would not be greater than the |
||||
|
range (because `2.0.1` satisfies, which is higher), nor less than the |
||||
|
range (since `1.2.8` satisfies, which is lower), and it also does not |
||||
|
satisfy the range. |
||||
|
|
||||
|
If you want to know if a version satisfies or does not satisfy a |
||||
|
range, use the `satisfies(version, range)` function. |
@ -0,0 +1,133 @@ |
|||||
|
#!/usr/bin/env node |
||||
|
// Standalone semver comparison program. |
||||
|
// Exits successfully and prints matching version(s) if |
||||
|
// any supplied version is valid and passes all tests. |
||||
|
|
||||
|
var argv = process.argv.slice(2) |
||||
|
, versions = [] |
||||
|
, range = [] |
||||
|
, gt = [] |
||||
|
, lt = [] |
||||
|
, eq = [] |
||||
|
, inc = null |
||||
|
, version = require("../package.json").version |
||||
|
, loose = false |
||||
|
, identifier = undefined |
||||
|
, semver = require("../semver") |
||||
|
, reverse = false |
||||
|
|
||||
|
main() |
||||
|
|
||||
|
function main () { |
||||
|
if (!argv.length) return help() |
||||
|
while (argv.length) { |
||||
|
var a = argv.shift() |
||||
|
var i = a.indexOf('=') |
||||
|
if (i !== -1) { |
||||
|
a = a.slice(0, i) |
||||
|
argv.unshift(a.slice(i + 1)) |
||||
|
} |
||||
|
switch (a) { |
||||
|
case "-rv": case "-rev": case "--rev": case "--reverse": |
||||
|
reverse = true |
||||
|
break |
||||
|
case "-l": case "--loose": |
||||
|
loose = true |
||||
|
break |
||||
|
case "-v": case "--version": |
||||
|
versions.push(argv.shift()) |
||||
|
break |
||||
|
case "-i": case "--inc": case "--increment": |
||||
|
switch (argv[0]) { |
||||
|
case "major": case "minor": case "patch": case "prerelease": |
||||
|
case "premajor": case "preminor": case "prepatch": |
||||
|
inc = argv.shift() |
||||
|
break |
||||
|
default: |
||||
|
inc = "patch" |
||||
|
break |
||||
|
} |
||||
|
break |
||||
|
case "--preid": |
||||
|
identifier = argv.shift() |
||||
|
break |
||||
|
case "-r": case "--range": |
||||
|
range.push(argv.shift()) |
||||
|
break |
||||
|
case "-h": case "--help": case "-?": |
||||
|
return help() |
||||
|
default: |
||||
|
versions.push(a) |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
versions = versions.filter(function (v) { |
||||
|
return semver.valid(v, loose) |
||||
|
}) |
||||
|
if (!versions.length) return fail() |
||||
|
if (inc && (versions.length !== 1 || range.length)) |
||||
|
return failInc() |
||||
|
|
||||
|
for (var i = 0, l = range.length; i < l ; i ++) { |
||||
|
versions = versions.filter(function (v) { |
||||
|
return semver.satisfies(v, range[i], loose) |
||||
|
}) |
||||
|
if (!versions.length) return fail() |
||||
|
} |
||||
|
return success(versions) |
||||
|
} |
||||
|
|
||||
|
function failInc () { |
||||
|
console.error("--inc can only be used on a single version with no range") |
||||
|
fail() |
||||
|
} |
||||
|
|
||||
|
function fail () { process.exit(1) } |
||||
|
|
||||
|
function success () { |
||||
|
var compare = reverse ? "rcompare" : "compare" |
||||
|
versions.sort(function (a, b) { |
||||
|
return semver[compare](a, b, loose) |
||||
|
}).map(function (v) { |
||||
|
return semver.clean(v, loose) |
||||
|
}).map(function (v) { |
||||
|
return inc ? semver.inc(v, inc, loose, identifier) : v |
||||
|
}).forEach(function (v,i,_) { console.log(v) }) |
||||
|
} |
||||
|
|
||||
|
function help () { |
||||
|
console.log(["SemVer " + version |
||||
|
,"" |
||||
|
,"A JavaScript implementation of the http://semver.org/ specification" |
||||
|
,"Copyright Isaac Z. Schlueter" |
||||
|
,"" |
||||
|
,"Usage: semver [options] <version> [<version> [...]]" |
||||
|
,"Prints valid versions sorted by SemVer precedence" |
||||
|
,"" |
||||
|
,"Options:" |
||||
|
,"-r --range <range>" |
||||
|
," Print versions that match the specified range." |
||||
|
,"" |
||||
|
,"-i --increment [<level>]" |
||||
|
," Increment a version by the specified level. Level can" |
||||
|
," be one of: major, minor, patch, premajor, preminor," |
||||
|
," prepatch, or prerelease. Default level is 'patch'." |
||||
|
," Only one version may be specified." |
||||
|
,"" |
||||
|
,"--preid <identifier>" |
||||
|
," Identifier to be used to prefix premajor, preminor," |
||||
|
," prepatch or prerelease version increments." |
||||
|
,"" |
||||
|
,"-l --loose" |
||||
|
," Interpret versions and ranges loosely" |
||||
|
,"" |
||||
|
,"Program exits successfully if any valid version satisfies" |
||||
|
,"all supplied ranges, and prints all satisfying versions." |
||||
|
,"" |
||||
|
,"If no satisfying versions are found, then exits failure." |
||||
|
,"" |
||||
|
,"Versions are printed in ascending order, so supplying" |
||||
|
,"multiple versions to the utility will just sort them." |
||||
|
].join("\n")) |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
{ |
||||
|
"_from": "semver@^5.3.0", |
||||
|
"_id": "semver@5.3.0", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha1-myzl094C0XxgEq0yaqa00M9U+U8=", |
||||
|
"_location": "/eslint/semver", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "semver@^5.3.0", |
||||
|
"name": "semver", |
||||
|
"escapedName": "semver", |
||||
|
"rawSpec": "^5.3.0", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^5.3.0" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz", |
||||
|
"_shasum": "9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f", |
||||
|
"_spec": "semver@^5.3.0", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint", |
||||
|
"bin": { |
||||
|
"semver": "./bin/semver" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/npm/node-semver/issues" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"deprecated": false, |
||||
|
"description": "The semantic version parser used by npm.", |
||||
|
"devDependencies": { |
||||
|
"tap": "^2.0.0" |
||||
|
}, |
||||
|
"files": [ |
||||
|
"bin", |
||||
|
"range.bnf", |
||||
|
"semver.js" |
||||
|
], |
||||
|
"homepage": "https://github.com/npm/node-semver#readme", |
||||
|
"license": "ISC", |
||||
|
"main": "semver.js", |
||||
|
"name": "semver", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git+https://github.com/npm/node-semver.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"test": "tap test/*.js" |
||||
|
}, |
||||
|
"version": "5.3.0" |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
range-set ::= range ( logical-or range ) * |
||||
|
logical-or ::= ( ' ' ) * '||' ( ' ' ) * |
||||
|
range ::= hyphen | simple ( ' ' simple ) * | '' |
||||
|
hyphen ::= partial ' - ' partial |
||||
|
simple ::= primitive | partial | tilde | caret |
||||
|
primitive ::= ( '<' | '>' | '>=' | '<=' | '=' | ) partial |
||||
|
partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )? |
||||
|
xr ::= 'x' | 'X' | '*' | nr |
||||
|
nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) * |
||||
|
tilde ::= '~' partial |
||||
|
caret ::= '^' partial |
||||
|
qualifier ::= ( '-' pre )? ( '+' build )? |
||||
|
pre ::= parts |
||||
|
build ::= parts |
||||
|
parts ::= part ( '.' part ) * |
||||
|
part ::= nr | [-0-9A-Za-z]+ |
File diff suppressed because it is too large
@ -0,0 +1,19 @@ |
|||||
|
'use strict'; |
||||
|
var shebangRegex = require('shebang-regex'); |
||||
|
|
||||
|
module.exports = function (str) { |
||||
|
var match = str.match(shebangRegex); |
||||
|
|
||||
|
if (!match) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var arr = match[0].replace(/#! ?/, '').split(' '); |
||||
|
var bin = arr[0].split('/').pop(); |
||||
|
var arg = arr[1]; |
||||
|
|
||||
|
return (bin === 'env' ? |
||||
|
arg : |
||||
|
bin + (arg ? ' ' + arg : '') |
||||
|
); |
||||
|
}; |
@ -0,0 +1,21 @@ |
|||||
|
The MIT License (MIT) |
||||
|
|
||||
|
Copyright (c) Kevin Martensson <kevinmartensson@gmail.com> (github.com/kevva) |
||||
|
|
||||
|
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. |
@ -0,0 +1,71 @@ |
|||||
|
{ |
||||
|
"_from": "shebang-command@^1.2.0", |
||||
|
"_id": "shebang-command@1.2.0", |
||||
|
"_inBundle": false, |
||||
|
"_integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", |
||||
|
"_location": "/eslint/shebang-command", |
||||
|
"_phantomChildren": {}, |
||||
|
"_requested": { |
||||
|
"type": "range", |
||||
|
"registry": true, |
||||
|
"raw": "shebang-command@^1.2.0", |
||||
|
"name": "shebang-command", |
||||
|
"escapedName": "shebang-command", |
||||
|
"rawSpec": "^1.2.0", |
||||
|
"saveSpec": null, |
||||
|
"fetchSpec": "^1.2.0" |
||||
|
}, |
||||
|
"_requiredBy": [ |
||||
|
"/eslint/cross-spawn" |
||||
|
], |
||||
|
"_resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", |
||||
|
"_shasum": "44aac65b695b03398968c39f363fee5deafdf1ea", |
||||
|
"_spec": "shebang-command@^1.2.0", |
||||
|
"_where": "/Users/trott/io.js/tools/eslint-tmp/node_modules/eslint/node_modules/cross-spawn", |
||||
|
"author": { |
||||
|
"name": "Kevin Martensson", |
||||
|
"email": "kevinmartensson@gmail.com", |
||||
|
"url": "github.com/kevva" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/kevva/shebang-command/issues" |
||||
|
}, |
||||
|
"bundleDependencies": false, |
||||
|
"dependencies": { |
||||
|
"shebang-regex": "^1.0.0" |
||||
|
}, |
||||
|
"deprecated": false, |
||||
|
"description": "Get the command from a shebang", |
||||
|
"devDependencies": { |
||||
|
"ava": "*", |
||||
|
"xo": "*" |
||||
|
}, |
||||
|
"engines": { |
||||
|
"node": ">=0.10.0" |
||||
|
}, |
||||
|
"files": [ |
||||
|
"index.js" |
||||
|
], |
||||
|
"homepage": "https://github.com/kevva/shebang-command#readme", |
||||
|
"keywords": [ |
||||
|
"cmd", |
||||
|
"command", |
||||
|
"parse", |
||||
|
"shebang" |
||||
|
], |
||||
|
"license": "MIT", |
||||
|
"name": "shebang-command", |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git+https://github.com/kevva/shebang-command.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"test": "xo && ava" |
||||
|
}, |
||||
|
"version": "1.2.0", |
||||
|
"xo": { |
||||
|
"ignores": [ |
||||
|
"test.js" |
||||
|
] |
||||
|
} |
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
# shebang-command [![Build Status](https://travis-ci.org/kevva/shebang-command.svg?branch=master)](https://travis-ci.org/kevva/shebang-command) |
||||
|
|
||||
|
> Get the command from a shebang |
||||
|
|
||||
|
|
||||
|
## Install |
||||
|
|
||||
|
``` |
||||
|
$ npm install --save shebang-command |
||||
|
``` |
||||
|
|
||||
|
|
||||
|
## Usage |
||||
|
|
||||
|
```js |
||||
|
const shebangCommand = require('shebang-command'); |
||||
|
|
||||
|
shebangCommand('#!/usr/bin/env node'); |
||||
|
//=> 'node' |
||||
|
|
||||
|
shebangCommand('#!/bin/bash'); |
||||
|
//=> 'bash' |
||||
|
``` |
||||
|
|
||||
|
|
||||
|
## API |
||||
|
|
||||
|
### shebangCommand(string) |
||||
|
|
||||
|
#### string |
||||
|
|
||||
|
Type: `string` |
||||
|
|
||||
|
String containing a shebang. |
||||
|
|
||||
|
|
||||
|
## License |
||||
|
|
||||
|
MIT © [Kevin Martensson](http://github.com/kevva) |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue