diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2016-09-08 04:22:16 +0300 |
---|---|---|
committer | Kat Marchán <kzm@sykosomatic.org> | 2016-09-09 03:05:54 +0300 |
commit | 242babbd02274ee2d212ae143992c20f47ef0066 (patch) | |
tree | e404ba7deae80663e056e60de3f4f450bd7041f6 | |
parent | 2b88d62e6a730716b27052c0911c094d01830a60 (diff) |
once@1.4.0
Added `once.strict`
Credit: @zkochan
-rw-r--r-- | node_modules/once/README.md | 28 | ||||
-rw-r--r-- | node_modules/once/once.js | 21 | ||||
-rw-r--r-- | node_modules/once/package.json | 72 | ||||
-rw-r--r-- | package.json | 2 |
4 files changed, 93 insertions, 30 deletions
diff --git a/node_modules/once/README.md b/node_modules/once/README.md index a2981ea07..1f1ffca93 100644 --- a/node_modules/once/README.md +++ b/node_modules/once/README.md @@ -49,3 +49,31 @@ function load (cb) { }) } ``` + +## `once.strict(func)` + +Throw an error if the function is called twice. + +Some functions are expected to be called only once. Using `once` for them would +potentially hide logical errors. + +In the example below, the `greet` function has to call the callback only once: + +```javascript +function greet (name, cb) { + // return is missing from the if statement + // when no name is passed, the callback is called twice + if (!name) cb('Hello anonymous') + cb('Hello ' + name) +} + +function log (msg) { + console.log(msg) +} + +// this will print 'Hello anonymous' but the logical error will be missed +greet(null, once(msg)) + +// once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time +greet(null, once.strict(msg)) +``` diff --git a/node_modules/once/once.js b/node_modules/once/once.js index 2e1e721bf..235406736 100644 --- a/node_modules/once/once.js +++ b/node_modules/once/once.js @@ -1,5 +1,6 @@ var wrappy = require('wrappy') module.exports = wrappy(once) +module.exports.strict = wrappy(onceStrict) once.proto = once(function () { Object.defineProperty(Function.prototype, 'once', { @@ -8,6 +9,13 @@ once.proto = once(function () { }, configurable: true }) + + Object.defineProperty(Function.prototype, 'onceStrict', { + value: function () { + return onceStrict(this) + }, + configurable: true + }) }) function once (fn) { @@ -19,3 +27,16 @@ function once (fn) { f.called = false return f } + +function onceStrict (fn) { + var f = function () { + if (f.called) + throw new Error(f.onceError) + f.called = true + return f.value = fn.apply(this, arguments) + } + var name = fn.name || 'Function wrapped with `once`' + f.onceError = name + " shouldn't be called more than once" + f.called = false + return f +} diff --git a/node_modules/once/package.json b/node_modules/once/package.json index f5fd8173c..1be563b51 100644 --- a/node_modules/once/package.json +++ b/node_modules/once/package.json @@ -1,50 +1,64 @@ { "_args": [ [ - "once@~1.3.3", - "/Users/ogd/Documents/projects/npm/npm" + { + "raw": "once@1.4.0", + "scope": null, + "escapedName": "once", + "name": "once", + "rawSpec": "1.4.0", + "spec": "1.4.0", + "type": "version" + }, + "/Users/zkat/Documents/code/npm" ] ], - "_from": "once@>=1.3.3 <1.4.0", - "_id": "once@1.3.3", + "_from": "once@1.4.0", + "_id": "once@1.4.0", "_inCache": true, - "_installable": true, "_location": "/once", - "_nodeVersion": "4.0.0", + "_nodeVersion": "6.5.0", + "_npmOperationalInternal": { + "host": "packages-12-west.internal.npmjs.com", + "tmp": "tmp/once-1.4.0.tgz_1473196269128_0.537820661207661" + }, "_npmUser": { - "email": "i@izs.me", - "name": "isaacs" + "name": "isaacs", + "email": "i@izs.me" }, - "_npmVersion": "3.3.2", + "_npmVersion": "3.10.7", "_phantomChildren": {}, "_requested": { - "name": "once", - "raw": "once@~1.3.3", - "rawSpec": "~1.3.3", + "raw": "once@1.4.0", "scope": null, - "spec": ">=1.3.3 <1.4.0", - "type": "range" + "escapedName": "once", + "name": "once", + "rawSpec": "1.4.0", + "spec": "1.4.0", + "type": "version" }, "_requiredBy": [ + "#USER", "/", "/glob", "/inflight", - "/node-gyp/glob", + "/init-package-json/glob", "/npm-registry-client", + "/read-package-json/glob", "/read-package-tree", "/readdir-scoped-modules", - "/standard/standard-engine/eslint/file-entry-cache/flat-cache/del/globby/glob", - "/standard/standard-engine/eslint/inquirer/run-async", - "/tap/nyc/istanbul" + "/standard/eslint/file-entry-cache/flat-cache/del/globby/glob", + "/standard/eslint/glob", + "/standard/eslint/inquirer/run-async" ], - "_resolved": "https://registry.npmjs.org/once/-/once-1.3.3.tgz", - "_shasum": "b2e261557ce4c314ec8304f3fa82663e4297ca20", + "_resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "_shasum": "583b1aa775961d4b113ac17d9c50baef9dd76bd1", "_shrinkwrap": null, - "_spec": "once@~1.3.3", - "_where": "/Users/ogd/Documents/projects/npm/npm", + "_spec": "once@1.4.0", + "_where": "/Users/zkat/Documents/code/npm", "author": { - "email": "i@izs.me", "name": "Isaac Z. Schlueter", + "email": "i@izs.me", "url": "http://blog.izs.me/" }, "bugs": { @@ -55,23 +69,23 @@ }, "description": "Run a function exactly one time", "devDependencies": { - "tap": "^1.2.0" + "tap": "^7.0.1" }, "directories": { "test": "test" }, "dist": { - "shasum": "b2e261557ce4c314ec8304f3fa82663e4297ca20", - "tarball": "http://registry.npmjs.org/once/-/once-1.3.3.tgz" + "shasum": "583b1aa775961d4b113ac17d9c50baef9dd76bd1", + "tarball": "https://registry.npmjs.org/once/-/once-1.4.0.tgz" }, "files": [ "once.js" ], - "gitHead": "2ad558657e17fafd24803217ba854762842e4178", + "gitHead": "0e614d9f5a7e6f0305c625f6b581f6d80b33b8a6", "homepage": "https://github.com/isaacs/once#readme", "keywords": [ - "function", "once", + "function", "one", "single" ], @@ -93,5 +107,5 @@ "scripts": { "test": "tap test/*.js" }, - "version": "1.3.3" + "version": "1.4.0" } diff --git a/package.json b/package.json index 6959fd185..726083e76 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "npm-registry-client": "~7.2.1", "npm-user-validate": "~0.1.5", "npmlog": "~4.0.0", - "once": "~1.3.3", + "once": "~1.4.0", "opener": "~1.4.1", "osenv": "~0.1.3", "path-is-inside": "~1.0.1", |