[Pkg-javascript-commits] [node-array-reduce] 01/04: Import Upstream version 0.0.0
Shirish Togarla
shirish12-guest at moszumanska.debian.org
Tue Feb 7 19:09:49 UTC 2017
This is an automated email from the git hooks/post-receive script.
shirish12-guest pushed a commit to branch master
in repository node-array-reduce.
commit 4a15baaad7ac4faeff27140dca50a9e3f8058312
Author: Shirish Togarla <shirishtogarla533 at gmail.com>
Date: Mon Feb 6 11:53:15 2017 +0000
Import Upstream version 0.0.0
---
.travis.yml | 4 +++
LICENSE | 18 ++++++++++++
example/sum.js | 4 +++
index.js | 18 ++++++++++++
package.json | 48 ++++++++++++++++++++++++++++++++
readme.markdown | 46 +++++++++++++++++++++++++++++++
test/reduce.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 223 insertions(+)
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..cc4dba2
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ee27ba4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,18 @@
+This software is released under the MIT license:
+
+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.
diff --git a/example/sum.js b/example/sum.js
new file mode 100644
index 0000000..ede09c8
--- /dev/null
+++ b/example/sum.js
@@ -0,0 +1,4 @@
+var reduce = require('../');
+var xs = [ 1, 2, 3, 4 ];
+var sum = reduce(xs, function (acc, x) { return acc + x }, 0);
+console.log(sum);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..2f35849
--- /dev/null
+++ b/index.js
@@ -0,0 +1,18 @@
+var hasOwn = Object.prototype.hasOwnProperty;
+
+module.exports = function (xs, f, acc) {
+ var hasAcc = arguments.length >= 3;
+ if (hasAcc && xs.reduce) return xs.reduce(f, acc);
+ if (xs.reduce) return xs.reduce(f);
+
+ for (var i = 0; i < xs.length; i++) {
+ if (!hasOwn.call(xs, i)) continue;
+ if (!hasAcc) {
+ acc = xs[i];
+ hasAcc = true;
+ continue;
+ }
+ acc = f(acc, xs[i], i);
+ }
+ return acc;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..ca2429e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,48 @@
+{
+ "name": "array-reduce",
+ "version": "0.0.0",
+ "description": "`[].reduce()` for old browsers",
+ "main": "index.js",
+ "devDependencies": {
+ "tape": "~2.3.2"
+ },
+ "scripts": {
+ "test": "tape test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/substack/array-reduce.git"
+ },
+ "homepage": "https://github.com/substack/array-reduce",
+ "keywords": [
+ "array",
+ "reduce",
+ "es5",
+ "ie6",
+ "ie7",
+ "ie8",
+ "fold"
+ ],
+ "author": {
+ "name": "James Halliday",
+ "email": "mail at substack.net",
+ "url": "http://substack.net"
+ },
+ "license": "MIT",
+ "testling": {
+ "files": "test/*.js",
+ "browsers": [
+ "ie/8..latest",
+ "firefox/16..latest",
+ "firefox/nightly",
+ "chrome/22..latest",
+ "chrome/canary",
+ "opera/12..latest",
+ "opera/next",
+ "safari/5.1..latest",
+ "ipad/6.0..latest",
+ "iphone/6.0..latest",
+ "android-browser/4.2..latest"
+ ]
+ }
+}
diff --git a/readme.markdown b/readme.markdown
new file mode 100644
index 0000000..395ab98
--- /dev/null
+++ b/readme.markdown
@@ -0,0 +1,46 @@
+# array-reduce
+
+`[].reduce()` for old browsers
+
+[![testling badge](https://ci.testling.com/substack/array-reduce.png)](https://ci.testling.com/substack/array-reduce)
+
+[![build status](https://secure.travis-ci.org/substack/array-reduce.png)](http://travis-ci.org/substack/array-reduce)
+
+# example
+
+```
+var reduce = require('array-reduce');
+var xs = [ 1, 2, 3, 4 ];
+var sum = reduce(xs, function (acc, x) { return acc + x }, 0);
+console.log(sum);
+```
+
+output:
+
+```
+10
+```
+
+# methods
+
+``` js
+var reduce = require('array-reduce')
+```
+
+## var res = reduce(xs, f, init)
+
+Create a result `res` by folding `acc = f(acc, xs[i], i)` over each element in
+the array `xs` at element `i`. If `init` is given, the first `acc` value is
+`init`, otherwise `xs[0]` is used.
+
+# install
+
+With [npm](https://npmjs.org) do:
+
+```
+npm install array-reduce
+```
+
+# license
+
+MIT
diff --git a/test/reduce.js b/test/reduce.js
new file mode 100644
index 0000000..0014b98
--- /dev/null
+++ b/test/reduce.js
@@ -0,0 +1,85 @@
+var reduce = require('../');
+var test = require('tape');
+
+test('numeric reduces', function (t) {
+ t.plan(6);
+
+ var xs = [ 1, 2, 3, 4 ];
+ t.equal(
+ reduce(xs, function (acc, x) { return acc + x }, 0),
+ 10
+ );
+ t.equal(
+ reduce(xs, function (acc, x) { return acc + x }, 100),
+ 110
+ );
+ t.equal(
+ reduce(xs, function (acc, x) { return acc + x }),
+ 10
+ );
+
+ var ys = cripple([ 1, 2, 3, 4 ]);
+ t.equal(
+ reduce(ys, function (acc, x) { return acc + x }, 0),
+ 10
+ );
+ t.equal(
+ reduce(ys, function (acc, x) { return acc + x }, 100),
+ 110
+ );
+ t.equal(
+ reduce(ys, function (acc, x) { return acc + x }),
+ 10
+ );
+});
+
+test('holes', function (t) {
+ t.plan(4);
+
+ var xs = Array(10);
+ xs[2] = 5; xs[4] = 6; xs[8] = 4;
+ t.equal(
+ reduce(xs, function (acc, x) { return acc + x }),
+ 15
+ );
+ t.equal(
+ reduce(xs, function (acc, x) { return acc + x }, 100),
+ 115
+ );
+
+ var ys = cripple(Array(10));
+ ys[2] = 5; ys[4] = 6; ys[8] = 4;
+ t.equal(
+ reduce(ys, function (acc, x) { return acc + x }),
+ 15
+ );
+ t.equal(
+ reduce(ys, function (acc, x) { return acc + x }, 100),
+ 115
+ );
+});
+
+test('object', function (t) {
+ t.plan(1);
+ var obj = { a: 3, b: 4, c: 5 };
+ var res = reduce(objectKeys(obj), function (acc, key) {
+ acc[key.toUpperCase()] = obj[key] * 111;
+ return acc;
+ }, {});
+ t.deepEqual(res, { A: 333, B: 444, C: 555 });
+});
+
+function cripple (xs) {
+ xs.reduce = undefined;
+ return xs;
+}
+
+var objectKeys = function (obj) {
+ var keys = [];
+ for (var key in obj) {
+ if (hasOwn.call(obj, key)) keys.push(key);
+ }
+ return keys;
+};
+
+var hasOwn = Object.prototype.hasOwnProperty;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-array-reduce.git
More information about the Pkg-javascript-commits
mailing list