Skip to content

Commit

Permalink
add-comments-to-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Rasmussen committed Jan 17, 2017
1 parent 5aa150f commit b80393c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ dotProp.delete(obj, 'foo.0.bar');

### toggle

Delete a value by a dot path.
Toggle a boolean a value by a dot path.

```javascript
var obj = {foo: { bar: true } };
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

/**
* Set a value by a dot path.
* @param obj The object to evaluate.
* @param prop The path to be set.
* @param val The value to set.
*/
module.exports.set = function(obj, prop, value) {
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

Expand All @@ -23,6 +29,11 @@ module.exports.set = function(obj, prop, value) {
return setPropImmutableRec(obj, prop, value, 0);
};

/**
* Get a value by a dot path.
* @param obj The object to evaluate.
* @param prop The path to value that should be returned.
*/
module.exports.get = function(obj, prop) {
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

Expand All @@ -40,6 +51,14 @@ module.exports.get = function(obj, prop) {
return obj;
};

/**
* Delete a property by a dot path.
* If target container is an object, the property is deleted.
* If target container is an array, the index is deleted.
* If target container is undefined, nothing is deleted.
* @param obj The object to evaluate.
* @param prop The path to the property or index that should be deleted.
*/
module.exports.delete = function(obj, prop) {
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

Expand Down Expand Up @@ -98,6 +117,7 @@ module.exports.toggle = function(obj, prop) {
* If target is null or undefined, the value is simply set.
* @param obj The object to evaluate.
* @param prop The path to the value.
* @param val The value to merge into the target value.
*/
module.exports.merge = function(obj, prop, val) {
var curVal = this.get(obj, prop);
Expand Down
22 changes: 21 additions & 1 deletion test/examples.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,32 @@ describe('examples.spec.js', function() {

describe('when delete', function() {

it('Array element', function() {
it('Array element by index', function() {
expect(dotProp.delete({foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}, 'foo.1')).to.eql(
{foo: [{ bar: 'gold-unicorn'}, 'silver-unicorn']}
);
});

it('Array element by $end', function() {
expect(dotProp.delete({foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}, 'foo.$end')).to.eql(
{foo: [{ bar: 'gold-unicorn'}, 'white-unicorn']}
);
});

it('Out of array', function() {
expect(dotProp.delete({foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}, 'foo.10')).to.eql(
{foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}
);
});

it('Array indexed by a property', function() {
try {
dotProp.delete({foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}, 'foo.bar');
} catch (err) {
expect(err).to.eql(new Error('Array index \'bar\' has to be an integer'));
}
});

it('Deep prop', function() {
expect(dotProp.delete({foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']}, 'foo.0.bar')).to.eql(
{foo: [{}, 'white-unicorn', 'silver-unicorn']}
Expand Down

0 comments on commit b80393c

Please sign in to comment.