Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Rasmussen committed Nov 13, 2015
1 parent 4c2122d commit e0f8216
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 12 deletions.
8 changes: 0 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"parser": "babel-eslint",
"rules": {
"camelcase": 0,
"consistent-return": 0,
Expand All @@ -11,10 +10,7 @@
"object-shorthand": 0,
"one-var": 0,
"quotes": [2, "single"],
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
"semi": [2, "always"],
"strict": [2, "never"],
"wrap-iife": [2, "inside"],
"new-cap": [2, {"capIsNew": false}],
"no-alert": 0,
Expand Down Expand Up @@ -43,11 +39,7 @@
"mocha": true,
"es6": true
},
"plugins": [
"react"
],
"ecmaFeatures": {
"jsx": true,
"modules": true
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dot-prop-immutable

{{description}}.
Immutable version of dot-prop with some extensions. The immutable part is inspired by React Immutability Helpers.

npm install dot-prop-immutable

Expand Down
58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

function setProp(obj, prop, value) {
prop = typeof prop === 'string' ? prop.split('.') : prop;

var setPropImmutableRec = function(obj, prop, value, i) {
var head = prop[i];

if (prop.length - 1 !== i) {
if (Array.isArray(obj)) {
if (/^\+?\d+$/.test(head) && obj.length > head) {
return [...obj.slice(0, head), setPropImmutableRec(obj[head] || {}, prop, value, i + 1), ...obj.slice(head + 1)];
} else {
let clone = [...obj];
clone[head] = setPropImmutableRec(obj[head] || {}, prop, value, i + 1);
return clone;
}

} else {
return Object.assign({}, obj, {[head]: setPropImmutableRec(obj[head] || {}, prop, value, i + 1)});
}
}

if (Array.isArray(obj)) {
if (/^\+?\d+$/.test(head) && obj.length > head) {
return [...obj.slice(0, head), typeof value === 'function' ? value(obj[head]) : value, ...obj.slice(head + 1)];
} else {
let clone = [...obj];
clone[head] = typeof value === 'function' ? value(obj[head]) : value;
return clone;
}

} else {

return Object.assign({}, obj, {[head]: typeof value === 'function' ? value(obj[head]) : value});
}
};

return setPropImmutableRec(obj, prop, value, 0);
}

function getProp(obj, prop) {
prop = typeof prop === 'string' ? prop.split('.') : prop;

for (var i = 0; i < prop.length; i++) {
if (typeof obj !== 'object') {
return undefined;
}
obj = obj[prop[i]];
}

return obj;
}

module.exports = {
set: setProp,
get: getProp
};
22 changes: 19 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "dot-prop-immutable",
"version": "1.0.0",
"description": "{{description}}",
"description": "Immutable version of dot-prop with some extensions",
"main": "index.js",
"engines": {
"node": "4.2.0",
"npm": "3.3.6"
},
"scripts": {
"test": "mocha"
"test": "mocha --harmony"
},
"repository": {
"type": "git",
Expand All @@ -14,9 +18,21 @@
"bugs": {
"url": "https://github.com/debitoor/dot-prop-immutable/issues"
},
"keywords": [
"immutable",
"obj",
"object",
"prop",
"property",
"dot",
"path",
"get",
"access",
"notation",
"dot-prop"
],
"homepage": "https://github.com/debitoor/dot-prop-immutable",
"devDependencies": {
"babel-eslint": "^4.1.3",
"chai": "^2.2.0",
"eslint-plugin-react": "^3.5.1",
"mocha": "^2.1.1",
Expand Down
10 changes: 10 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"rules": {
"no-unused-expressions": 0,
"no-unused-vars": 0,
"no-trailing-spaces": 0
},
"globals": {
"expect": true
}
}
Loading

0 comments on commit e0f8216

Please sign in to comment.