[Pkg-javascript-commits] [node-getobject] 01/04: Imported Upstream version 0.1.0
Mattia Rizzolo
mattia at debian.org
Thu Jan 7 23:46:11 UTC 2016
This is an automated email from the git hooks/post-receive script.
mattia pushed a commit to branch master
in repository node-getobject.
commit e738a125e24d066f70ee10a9d7f7e76cf110fd29
Author: Jonathan Horn <jonathan at jonathan-debian>
Date: Fri Jan 8 00:19:15 2016 +0100
Imported Upstream version 0.1.0
---
.gitignore | 1 +
.jshintrc | 15 +++++++++++++
.travis.yml | 6 +++++
Gruntfile.js | 48 ++++++++++++++++++++++++++++++++++++++++
LICENSE-MIT | 22 ++++++++++++++++++
README.md | 20 +++++++++++++++++
lib/getobject.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 44 ++++++++++++++++++++++++++++++++++++
test/namespace_test.js | 51 ++++++++++++++++++++++++++++++++++++++++++
9 files changed, 267 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2ccbe46
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/node_modules/
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..2c40c44
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,15 @@
+{
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "undef": true,
+ "unused": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true,
+ "es5": true
+}
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..cbace30
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
+before_script:
+ - npm install -g grunt-cli
diff --git a/Gruntfile.js b/Gruntfile.js
new file mode 100644
index 0000000..c3f7d74
--- /dev/null
+++ b/Gruntfile.js
@@ -0,0 +1,48 @@
+'use strict';
+
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ nodeunit: {
+ files: ['test/**/*_test.js'],
+ },
+ jshint: {
+ options: {
+ jshintrc: '.jshintrc'
+ },
+ gruntfile: {
+ src: 'Gruntfile.js'
+ },
+ lib: {
+ src: ['lib/**/*.js']
+ },
+ test: {
+ src: ['test/*.js']
+ },
+ },
+ watch: {
+ gruntfile: {
+ files: '<%= jshint.gruntfile.src %>',
+ tasks: ['jshint:gruntfile']
+ },
+ lib: {
+ files: '<%= jshint.lib.src %>',
+ tasks: ['jshint:lib', 'nodeunit']
+ },
+ test: {
+ files: '<%= jshint.test.src %>',
+ tasks: ['jshint:test', 'nodeunit']
+ },
+ },
+ });
+
+ // These plugins provide necessary tasks.
+ grunt.loadNpmTasks('grunt-contrib-nodeunit');
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-contrib-watch');
+
+ // Default task.
+ grunt.registerTask('default', ['jshint', 'nodeunit']);
+
+};
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..bb2aad6
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2013 "Cowboy" Ben Alman
+
+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..6d27c24
--- /dev/null
+++ b/README.md
@@ -0,0 +1,20 @@
+# getobject [![Build Status](https://secure.travis-ci.org/cowboy/node-getobject.png?branch=master)](http://travis-ci.org/cowboy/node-getobject)
+
+get.and.set.deep.objects.easily = true;
+
+## Getting Started
+Install the module with: `npm install getobject`
+
+```javascript
+var getobject = require('getobject');
+```
+
+## Contributing
+In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
+
+## Release History
+_(Nothing yet)_
+
+## License
+Copyright (c) 2013 "Cowboy" Ben Alman
+Licensed under the MIT license.
\ No newline at end of file
diff --git a/lib/getobject.js b/lib/getobject.js
new file mode 100644
index 0000000..e4006fe
--- /dev/null
+++ b/lib/getobject.js
@@ -0,0 +1,60 @@
+/*
+ * getobject
+ * https://github.com/cowboy/node-getobject
+ *
+ * Copyright (c) 2013 "Cowboy" Ben Alman
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+var getobject = module.exports = {};
+
+// Split strings on dot, but only if dot isn't preceded by a backslash. Since
+// JavaScript doesn't support lookbehinds, use a placeholder for "\.", split
+// on dot, then replace the placeholder character with a dot.
+function getParts(str) {
+ return str.replace(/\\\./g, '\uffff').split('.').map(function(s) {
+ return s.replace(/\uffff/g, '.');
+ });
+}
+
+// Get the value of a deeply-nested property exist in an object.
+getobject.get = function(obj, parts, create) {
+ if (typeof parts === 'string') {
+ parts = getParts(parts);
+ }
+
+ var part;
+ while (typeof obj === 'object' && obj && parts.length) {
+ part = parts.shift();
+ if (!(part in obj) && create) {
+ obj[part] = {};
+ }
+ obj = obj[part];
+ }
+
+ return obj;
+};
+
+// Set a deeply-nested property in an object, creating intermediary objects
+// as we go.
+getobject.set = function(obj, parts, value) {
+ parts = getParts(parts);
+
+ var prop = parts.pop();
+ obj = getobject.get(obj, parts, true);
+ if (obj && typeof obj === 'object') {
+ return (obj[prop] = value);
+ }
+};
+
+// Does a deeply-nested property exist in an object?
+getobject.exists = function(obj, parts) {
+ parts = getParts(parts);
+
+ var prop = parts.pop();
+ obj = getobject.get(obj, parts);
+
+ return typeof obj === 'object' && obj && prop in obj;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..364155f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,44 @@
+{
+ "name": "getobject",
+ "description": "get.and.set.deep.objects.easily = true",
+ "version": "0.1.0",
+ "homepage": "https://github.com/cowboy/node-getobject",
+ "author": {
+ "name": "\"Cowboy\" Ben Alman",
+ "url": "http://benalman.com/"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/cowboy/node-getobject.git"
+ },
+ "bugs": {
+ "url": "https://github.com/cowboy/node-getobject/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/cowboy/node-getobject/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "lib/getobject",
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "scripts": {
+ "test": "grunt nodeunit"
+ },
+ "devDependencies": {
+ "grunt-contrib-jshint": "~0.1.1",
+ "grunt-contrib-nodeunit": "~0.1.2",
+ "grunt-contrib-watch": "~0.2.0",
+ "grunt": "~0.4.1"
+ },
+ "keywords": [
+ "dot notation",
+ "properties",
+ "get",
+ "set",
+ "object",
+ "dot"
+ ]
+}
diff --git a/test/namespace_test.js b/test/namespace_test.js
new file mode 100644
index 0000000..2e1c2ae
--- /dev/null
+++ b/test/namespace_test.js
@@ -0,0 +1,51 @@
+'use strict';
+
+var getobject = require('../lib/getobject');
+
+exports.get = {
+ 'no create': function(test) {
+ var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
+ test.strictEqual(getobject.get(obj, 'a'), obj.a, 'should get immediate properties.');
+ test.strictEqual(getobject.get(obj, 'a.b'), obj.a.b, 'should get nested properties.');
+ test.strictEqual(getobject.get(obj, 'a.x'), undefined, 'should return undefined for nonexistent properties.');
+ test.strictEqual(getobject.get(obj, 'a.b.c'), 1, 'should return values.');
+ test.strictEqual(getobject.get(obj, 'a.b.d'), '', 'should return values.');
+ test.strictEqual(getobject.get(obj, 'a.b.e'), null, 'should return values.');
+ test.strictEqual(getobject.get(obj, 'a.b.f'), undefined, 'should return values.');
+ test.strictEqual(getobject.get(obj, 'a.b.g\\.h\\.i'), 2, 'literal backslash should escape period in property name.');
+ test.done();
+ },
+ 'create': function(test) {
+ var obj = {a: 1};
+ test.strictEqual(getobject.get(obj, 'a', true), obj.a, 'should just return existing properties.');
+ test.strictEqual(getobject.get(obj, 'b', true), obj.b, 'should create immediate properties.');
+ test.strictEqual(getobject.get(obj, 'c.d.e', true), obj.c.d.e, 'should create nested properties.');
+ test.done();
+ }
+};
+
+exports.set = function(test) {
+ var obj = {};
+ test.strictEqual(getobject.set(obj, 'a', 1), 1, 'should return immediate property value.');
+ test.strictEqual(obj.a, 1, 'should set property value.');
+ test.strictEqual(getobject.set(obj, 'b.c.d', 1), 1, 'should return nested property value.');
+ test.strictEqual(obj.b.c.d, 1, 'should set property value.');
+ test.strictEqual(getobject.set(obj, 'e\\.f\\.g', 1), 1, 'literal backslash should escape period in property name.');
+ test.strictEqual(obj['e.f.g'], 1, 'should set property value.');
+ test.done();
+};
+
+exports.exists = function(test) {
+ var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
+ test.ok(getobject.exists(obj, 'a'), 'immediate property should exist.');
+ test.ok(getobject.exists(obj, 'a.b'), 'nested property should exist.');
+ test.ok(getobject.exists(obj, 'a.b.c'), 'nested property should exist.');
+ test.ok(getobject.exists(obj, 'a.b.d'), 'nested property should exist.');
+ test.ok(getobject.exists(obj, 'a.b.e'), 'nested property should exist.');
+ test.ok(getobject.exists(obj, 'a.b.f'), 'nested property should exist.');
+ test.ok(getobject.exists(obj, 'a.b.g\\.h\\.i'), 'literal backslash should escape period in property name.');
+ test.equal(getobject.exists(obj, 'x'), false, 'nonexistent property should not exist.');
+ test.equal(getobject.exists(obj, 'a.x'), false, 'nonexistent property should not exist.');
+ test.equal(getobject.exists(obj, 'a.b.x'), false, 'nonexistent property should not exist.');
+ test.done();
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-getobject.git
More information about the Pkg-javascript-commits
mailing list