From b1cc55426269aaf574de084fbffc3e51db096f42 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 2 Jul 2020 16:00:40 +0300 Subject: [PATCH] initial commit... Signed-off-by: Alex A. Naanou --- .gitignore | 6 +++++ LICENSE | 29 ++++++++++++++++++++++++ README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 24 ++++++++++++++++++++ run.js | 30 +++++++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 package.json create mode 100644 run.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80885ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.* +*.vim +*.sw[po] +coverage +node_modules +npm-debug.log diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b3bd51a --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2020, Alex A. Naanou +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..906673d --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ +# object-run.js + +This module defines a singe method `.run(..)` on `Object.prototype` making +it visible from all JavaScript objects inheriting from Object. + +The `.run(..)` method simply executes a function in the context of object +from which `.run(..)` was called. + +The return value of `.run(..)` is the non-`undefined` return value of the +passed function. + +The use of `.run(..)` enables the concatinative programming style on any +JavaScript structure/object. + + +## Installation + +```shell +$ npm install --save object-run +``` + +## Basic usage + +```javascript +require('object-run') +``` + +```javascript +var o = {} + .run(function(){ + this.x = 123 + this.y = 321 + }) +``` + + + +## Components + +### `Object.prototype.run(..)` + +``` +.run() + -> + -> +``` + +``` +() + -> undefined + -> +``` + + +## License + +[BSD 3-Clause License](./LICENSE) + +Copyright (c) 2016-2020, Alex A. Naanou, +All rights reserved. + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..dcfd556 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "object-run", + "version": "1.0.0", + "description": "Implements .run(..) method on object.", + "main": "run.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/flynx/object-run.js.git" + }, + "keywords": [ + "OOP", + "JavaScript", + "Object" + ], + "author": "Alex A. Naanou ", + "license": "BSD-3-Clause", + "bugs": { + "url": "https://github.com/flynx/object-run.js/issues" + }, + "homepage": "https://github.com/flynx/object-run.js#readme" +} diff --git a/run.js b/run.js new file mode 100644 index 0000000..dae910f --- /dev/null +++ b/run.js @@ -0,0 +1,30 @@ +/********************************************************************** +* +* object-run.js +* +* Repo and docs: +* https://github.com/flynx/object-run.js +* +* +***********************************************/ /* c8 ignore next 2 */ +((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) +(function(require){ var module={} // make module AMD/node compatible... +/*********************************************************************/ + +// Run a function in the context of an object... +// +Object.prototype.run + || Object.defineProperty(Object.prototype, 'run', { + enumerable: false, + value: function(func){ + var res = func ? + func.call(this) + : undefined + return res === undefined ? + this + : res }, }) + + + +/********************************************************************** +* vim:set ts=4 sw=4 : */ return module })