[Pkg-javascript-commits] [node-defaults] 01/04: Import Upstream version 1.0.3

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Thu Oct 27 17:04:03 UTC 2016


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-defaults.

commit 3c4dc4784cc3beefea764caee893a7006bafdd70
Author: Suhail P <psuhailp at gmail.com>
Date:   Thu Oct 27 14:17:21 2016 +0000

    Import Upstream version 1.0.3
---
 .gitignore   |  1 +
 LICENSE      | 21 +++++++++++++++++++++
 README.md    | 43 +++++++++++++++++++++++++++++++++++++++++++
 index.js     | 13 +++++++++++++
 package.json | 26 ++++++++++++++++++++++++++
 test.js      | 34 ++++++++++++++++++++++++++++++++++
 6 files changed, 138 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d88b072
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Elijah Insua
+
+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/README.md b/README.md
new file mode 100644
index 0000000..1a4a2ea
--- /dev/null
+++ b/README.md
@@ -0,0 +1,43 @@
+# defaults
+
+A simple one level options merge utility
+
+## install
+
+`npm install defaults`
+
+## use
+
+```javascript
+
+var defaults = require('defaults');
+
+var handle = function(options, fn) {
+  options = defaults(options, {
+    timeout: 100
+  });
+
+  setTimeout(function() {
+    fn(options);
+  }, options.timeout);
+}
+
+handle({ timeout: 1000 }, function() {
+  // we're here 1000 ms later
+});
+
+handle({ timeout: 10000 }, function() {
+  // we're here 10s later
+});
+
+```
+
+## summary
+
+this module exports a function that takes 2 arguments: `options` and `defaults`.  When called, it overrides all of `undefined` properties in `options` with the clones of properties defined in `defaults`
+
+Sidecases: if called with a falsy `options` value, options will be initialized to a new object before being merged onto.
+
+## license
+
+[MIT](LICENSE)
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..cb7d75c
--- /dev/null
+++ b/index.js
@@ -0,0 +1,13 @@
+var clone = require('clone');
+
+module.exports = function(options, defaults) {
+  options = options || {};
+
+  Object.keys(defaults).forEach(function(key) {
+    if (typeof options[key] === 'undefined') {
+      options[key] = clone(defaults[key]);
+    }
+  });
+
+  return options;
+};
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..854016d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,26 @@
+{
+  "name": "defaults",
+  "version": "1.0.3",
+  "description": "merge single level defaults over a config object",
+  "main": "index.js",
+  "scripts": {
+    "test": "node test.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/tmpvar/defaults.git"
+  },
+  "keywords": [
+    "config",
+    "defaults"
+  ],
+  "author": "Elijah Insua <tmpvar at gmail.com>",
+  "license": "MIT",
+  "readmeFilename": "README.md",
+  "dependencies": {
+    "clone": "^1.0.2"
+  },
+  "devDependencies": {
+    "tap": "^2.0.0"
+  }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..60e0ffb
--- /dev/null
+++ b/test.js
@@ -0,0 +1,34 @@
+var defaults = require('./'),
+    test = require('tap').test;
+
+test("ensure options is an object", function(t) {
+  var options = defaults(false, { a : true });
+  t.ok(options.a);
+  t.end()
+});
+
+test("ensure defaults override keys", function(t) {
+  var result = defaults({}, { a: false, b: true });
+  t.ok(result.b, 'b merges over undefined');
+  t.equal(result.a, false, 'a merges over undefined');
+  t.end();
+});
+
+test("ensure defined keys are not overwritten", function(t) {
+  var result = defaults({ b: false }, { a: false, b: true });
+  t.equal(result.b, false, 'b not merged');
+  t.equal(result.a, false, 'a merges over undefined');
+  t.end();
+});
+
+test("ensure defaults clone nested objects", function(t) {
+  var d = { a: [1,2,3], b: { hello : 'world' } };
+  var result = defaults({}, d);
+  t.equal(result.a.length, 3, 'objects should be clones');
+  t.ok(result.a !== d.a, 'objects should be clones');
+
+  t.equal(Object.keys(result.b).length, 1, 'objects should be clones');
+  t.ok(result.b !== d.b, 'objects should be clones');
+  t.end();
+});
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-defaults.git



More information about the Pkg-javascript-commits mailing list