[Pkg-javascript-commits] [node-obj-util] 01/02: Imported Upstream version 2.0.0
Thorsten Alteholz
alteholz at moszumanska.debian.org
Sun Mar 27 15:35:42 UTC 2016
This is an automated email from the git hooks/post-receive script.
alteholz pushed a commit to branch master
in repository node-obj-util.
commit 98367c4169544d9e6edf471668767c365848324f
Author: Thorsten Alteholz <debian at alteholz.de>
Date: Sun Mar 27 17:35:39 2016 +0200
Imported Upstream version 2.0.0
---
.npmignore | 28 ++++++++++++++++++
LICENSE | 22 ++++++++++++++
README.md | 49 +++++++++++++++++++++++++++++++
index.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 29 ++++++++++++++++++
5 files changed, 224 insertions(+)
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..59d842b
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,28 @@
+# Logs
+logs
+*.log
+
+# Runtime data
+pids
+*.pid
+*.seed
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directory
+# Commenting this out is preferred by some people, see
+# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
+node_modules
+
+# Users Environment Variables
+.lock-wscript
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..c58c339
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Roy Riojas
+
+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..8b1f06b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,49 @@
+# obj-util
+> A simple helper to set/get keys from objects using a string path like 'some.key.here'
+
+## Install
+
+```bash
+npm i --save obj-util
+```
+
+## Usage
+
+### obj.getKeyValue
+
+Returns the value from the given object which matches the passed key
+- **param** obj {Object} The object to get the values from
+- **param** key {String} a string representing a key in the object. Subkeys are supported separating them with dots. i.e. `key1.subkey1.subsubkey1`
+- **returns** {Mixed} the value of the given key in the passed obj
+
+```javascript
+var objUtil = require('obj-util');
+
+var obj = {
+ some: {
+ key: 'some value'
+ }
+};
+
+// getKeyValue
+objUtil.getKeyValue(obj, 'some.key'); // 'some value'
+```
+
+### obj.setKeyValue
+
+Sets a value in an object if a matching key is found inside the given object
+
+- **param** obj {Object} the object where to set the value using if the key is found
+- **param** key {String} a string that represents the key. Subkeys are supported by separating them with dots.
+- **param** val the value to be set in the object
+
+```javascript
+var objUtil = require('obj-util');
+
+var obj = {
+};
+objUtil.setKeyValue(obj, 'some.key', 'some value');
+obj.some.key === 'some value' //==> true
+```
+
+That's it!
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..882c6c6
--- /dev/null
+++ b/index.js
@@ -0,0 +1,96 @@
+/**
+ *
+ * Module with utilities to set and retrieve values on an object using a `path`
+ * @class objUtil
+ * @static
+ */
+var objUtil = {
+ /**
+ * Returns the value from the given object which matches the passed key
+ *
+ * @method getKeyValue
+ * @param obj {Object} The object to get the values from
+ * @param key {String} a string representing a key in the object. Subkeys are supported
+ * separating them with dots. i.e. `key1.subkey1.subsubkey1`
+ * @returns {Mixed} the value of the given key in the passed obj
+ * @example
+ * ```javascript
+ * var objUtil = require('obj-util');
+ *
+ * var obj = {
+ * some: {
+ * key: 'some value'
+ * }
+ * };
+ *
+ * objUtil.getKeyValue(obj, 'some.key'); // 'some value'
+ *
+ * ```
+ */
+ getKeyValue: function ( obj, key ) {
+ if ( !obj ) {
+ return null;
+ }
+
+ var temp = obj,
+ keys = key.split( '.' );
+
+ for (var ix = 0; ix < keys.length; ix++) {
+ var theKey = keys[ ix ];
+ temp = temp[ theKey ];
+ if (typeof temp === 'undefined') {
+ return undefined;
+ }
+ if ( temp === null ) {
+ return null;
+ }
+ }
+ return temp;
+ },
+ /**
+ * Sets a value in an object if a matching key is found inside the given object
+ *
+ * @method setKeyValue
+ * @param obj {Object} the object where to set the value using if the key is found
+ * @param key {String} a string that represents the key. Subkeys are supported by separating them with dots.
+ * @param val the value to be set in the object
+ * @example
+ * ```javascript
+ * var objUtil = require('obj-util');
+ *
+ * var obj = {
+ * };
+ * objUtil.setKeyValue(obj, 'some.key', 'some value');
+ * obj.some.key === 'some value' //==> true
+ * ```
+ */
+ setKeyValue: function ( obj, key, val ) {
+ if ( !obj ) {
+ return;
+ }
+
+ var temp = obj,
+ keys = key.split( '.' );
+
+ for (var ix = 0, len = keys.length; ix < len; ix++) {
+ var theKey = keys[ ix ],
+ t = temp[ theKey ];
+
+ if ( typeof t === 'undefined' || t === null ) {
+ var isPrevLast = (ix < len - 1);
+
+ temp[ theKey ] = isPrevLast ? {} : val;
+
+ if ( isPrevLast ) {
+ temp = temp[ theKey ];
+ }
+ } else {
+ if ( ix < len ) {
+ temp = t;
+ }
+ }
+ }
+ }
+};
+
+module.exports = objUtil;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..3e2e82d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+ "name": "obj-util",
+ "version": "2.0.0",
+ "description": "A simple helper to set/get keys from objects using a string path like 'some.key.here'",
+ "main": "index.js",
+ "scripts": {
+ "test": "test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/royriojas/obj-util.git"
+ },
+ "keywords": [
+ "obj",
+ "util",
+ "setKey",
+ "getKey",
+ "set",
+ "key",
+ "using",
+ "path"
+ ],
+ "author": "Roy Riojas",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/royriojas/obj-util/issues"
+ },
+ "homepage": "https://github.com/royriojas/obj-util"
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-obj-util.git
More information about the Pkg-javascript-commits
mailing list