added unit testing, and started implementing unit tests...phew
This commit is contained in:
18
node_modules/pathval/CHANGELOG.md
generated
vendored
Normal file
18
node_modules/pathval/CHANGELOG.md
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
0.1.1 / 2013-12-30
|
||||
==================
|
||||
|
||||
* expose parse
|
||||
* rename lib to index
|
||||
|
||||
0.1.0 / 2013-12-28
|
||||
==================
|
||||
|
||||
* API BREAKING! `get` has been changed, see the README for migration path
|
||||
* Add `set` method
|
||||
* Start using simple-assert (closes #2)
|
||||
|
||||
0.0.1 / 2013-11-24
|
||||
==================
|
||||
|
||||
* Initial implementation
|
16
node_modules/pathval/LICENSE
generated
vendored
Normal file
16
node_modules/pathval/LICENSE
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2011-2013 Jake Luer jake@alogicalparadox.com
|
||||
|
||||
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.
|
145
node_modules/pathval/README.md
generated
vendored
Normal file
145
node_modules/pathval/README.md
generated
vendored
Normal file
@ -0,0 +1,145 @@
|
||||
<h1 align=center>
|
||||
<a href="http://chaijs.com" title="Chai Documentation">
|
||||
<img alt="ChaiJS" src="http://chaijs.com/img/chai-logo.png"/> pathval
|
||||
</a>
|
||||
</h1>
|
||||
|
||||
<p align=center>
|
||||
Tool for Object value retrieval given a string path for <a href="http://nodejs.org">node</a> and the browser.
|
||||
</p>
|
||||
|
||||
<p align=center>
|
||||
<a href="./LICENSE">
|
||||
<img
|
||||
alt="license:mit"
|
||||
src="https://img.shields.io/badge/license-mit-green.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="https://github.com/chaijs/pathval/releases">
|
||||
<img
|
||||
alt="tag:?"
|
||||
src="https://img.shields.io/github/tag/chaijs/pathval.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="https://travis-ci.org/chaijs/pathval">
|
||||
<img
|
||||
alt="build:?"
|
||||
src="https://img.shields.io/travis/chaijs/pathval/master.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="https://coveralls.io/r/chaijs/pathval">
|
||||
<img
|
||||
alt="coverage:?"
|
||||
src="https://img.shields.io/coveralls/chaijs/pathval/master.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="https://www.npmjs.com/packages/pathval">
|
||||
<img
|
||||
alt="npm:?"
|
||||
src="https://img.shields.io/npm/v/pathval.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="https://www.npmjs.com/packages/pathval">
|
||||
<img
|
||||
alt="dependencies:?"
|
||||
src="https://img.shields.io/npm/dm/pathval.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="">
|
||||
<img
|
||||
alt="devDependencies:?"
|
||||
src="https://img.shields.io/david/chaijs/pathval.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<br/>
|
||||
<a href="https://saucelabs.com/u/chaijs-pathval">
|
||||
<img
|
||||
alt="Selenium Test Status"
|
||||
src="https://saucelabs.com/browser-matrix/chaijs-pathval.svg"
|
||||
/>
|
||||
</a>
|
||||
<br>
|
||||
<a href="https://chai-slack.herokuapp.com/">
|
||||
<img
|
||||
alt="Join the Slack chat"
|
||||
src="https://img.shields.io/badge/slack-join%20chat-E2206F.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
<a href="https://gitter.im/chaijs/chai">
|
||||
<img
|
||||
alt="Join the Gitter chat"
|
||||
src="https://img.shields.io/badge/gitter-join%20chat-D0104D.svg?style=flat-square"
|
||||
/>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
## What is pathval?
|
||||
|
||||
Pathval is a module which you can use to retrieve or set an Object's property for a given `String` path.
|
||||
|
||||
## Installation
|
||||
|
||||
### Node.js
|
||||
|
||||
`pathval` is available on [npm](http://npmjs.org). To install it, type:
|
||||
|
||||
$ npm install pathval
|
||||
|
||||
### Browsers
|
||||
|
||||
You can also use it within the browser; install via npm and use the `pathval.js` file found within the download. For example:
|
||||
|
||||
```html
|
||||
<script src="./node_modules/pathval/pathval.js"></script>
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
The primary export of `pathval` is an object which has the following methods:
|
||||
|
||||
* `hasProperty(object, name)` - Checks whether an `object` has `name`d property or numeric array index.
|
||||
* `getPathInfo(object, path)` - Returns an object with info indicating the value of the `parent` of that path, the `name ` of the property we're retrieving and its `value`.
|
||||
* `getPathValue(object, path)` - Retrieves the value of a property at a given `path` inside an `object`'.
|
||||
* `setPathValue(object, path, value)` - Sets the `value` of a property at a given `path` inside an `object`'.
|
||||
|
||||
```js
|
||||
var pathval = require('pathval');
|
||||
```
|
||||
|
||||
#### .hasProperty(object, name)
|
||||
|
||||
```js
|
||||
var pathval = require('pathval');
|
||||
|
||||
var obj = { prop: 'a value' };
|
||||
pathval.hasProperty(obj, 'prop'); // true
|
||||
```
|
||||
|
||||
#### .getPathInfo(object, path)
|
||||
|
||||
```js
|
||||
var pathval = require('pathval');
|
||||
|
||||
var obj = { earth: { country: 'Brazil' } };
|
||||
pathval.getPathInfo(obj, 'earth.country'); // { parent: { country: 'Brazil' }, name: 'country', value: 'Brazil', exists: true }
|
||||
```
|
||||
|
||||
#### .getPathValue(object, path)
|
||||
|
||||
```js
|
||||
var pathval = require('pathval');
|
||||
|
||||
var obj = { earth: { country: 'Brazil' } };
|
||||
pathval.getPathValue(obj, 'earth.country'); // 'Brazil'
|
||||
```
|
||||
|
||||
#### .setPathValue(object, path, value)
|
||||
|
||||
```js
|
||||
var pathval = require('pathval');
|
||||
|
||||
var obj = { earth: { country: 'Brazil' } };
|
||||
pathval.setPathValue(obj, 'earth.country', 'USA');
|
||||
|
||||
obj.earth.country; // 'USA'
|
||||
```
|
291
node_modules/pathval/index.js
generated
vendored
Normal file
291
node_modules/pathval/index.js
generated
vendored
Normal file
@ -0,0 +1,291 @@
|
||||
'use strict';
|
||||
|
||||
/* !
|
||||
* Chai - pathval utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* @see https://github.com/logicalparadox/filtr
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### .hasProperty(object, name)
|
||||
*
|
||||
* This allows checking whether an object has own
|
||||
* or inherited from prototype chain named property.
|
||||
*
|
||||
* Basically does the same thing as the `in`
|
||||
* operator but works properly with null/undefined values
|
||||
* and other primitives.
|
||||
*
|
||||
* var obj = {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
*
|
||||
* The following would be the results.
|
||||
*
|
||||
* hasProperty(obj, 'str'); // true
|
||||
* hasProperty(obj, 'constructor'); // true
|
||||
* hasProperty(obj, 'bar'); // false
|
||||
*
|
||||
* hasProperty(obj.str, 'length'); // true
|
||||
* hasProperty(obj.str, 1); // true
|
||||
* hasProperty(obj.str, 5); // false
|
||||
*
|
||||
* hasProperty(obj.arr, 'length'); // true
|
||||
* hasProperty(obj.arr, 2); // true
|
||||
* hasProperty(obj.arr, 3); // false
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String|Symbol} name
|
||||
* @returns {Boolean} whether it exists
|
||||
* @namespace Utils
|
||||
* @name hasProperty
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function hasProperty(obj, name) {
|
||||
if (typeof obj === 'undefined' || obj === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The `in` operator does not work with primitives.
|
||||
return name in Object(obj);
|
||||
}
|
||||
|
||||
/* !
|
||||
* ## parsePath(path)
|
||||
*
|
||||
* Helper function used to parse string object
|
||||
* paths. Use in conjunction with `internalGetPathValue`.
|
||||
*
|
||||
* var parsed = parsePath('myobject.property.subprop');
|
||||
*
|
||||
* ### Paths:
|
||||
*
|
||||
* * Can be infinitely deep and nested.
|
||||
* * Arrays are also valid using the formal `myobject.document[3].property`.
|
||||
* * Literal dots and brackets (not delimiter) must be backslash-escaped.
|
||||
*
|
||||
* @param {String} path
|
||||
* @returns {Object} parsed
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function parsePath(path) {
|
||||
var str = path.replace(/([^\\])\[/g, '$1.[');
|
||||
var parts = str.match(/(\\\.|[^.]+?)+/g);
|
||||
return parts.map(function mapMatches(value) {
|
||||
var regexp = /^\[(\d+)\]$/;
|
||||
var mArr = regexp.exec(value);
|
||||
var parsed = null;
|
||||
if (mArr) {
|
||||
parsed = { i: parseFloat(mArr[1]) };
|
||||
} else {
|
||||
parsed = { p: value.replace(/\\([.\[\]])/g, '$1') };
|
||||
}
|
||||
|
||||
return parsed;
|
||||
});
|
||||
}
|
||||
|
||||
/* !
|
||||
* ## internalGetPathValue(obj, parsed[, pathDepth])
|
||||
*
|
||||
* Helper companion function for `.parsePath` that returns
|
||||
* the value located at the parsed address.
|
||||
*
|
||||
* var value = getPathValue(obj, parsed);
|
||||
*
|
||||
* @param {Object} object to search against
|
||||
* @param {Object} parsed definition from `parsePath`.
|
||||
* @param {Number} depth (nesting level) of the property we want to retrieve
|
||||
* @returns {Object|Undefined} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function internalGetPathValue(obj, parsed, pathDepth) {
|
||||
var temporaryValue = obj;
|
||||
var res = null;
|
||||
pathDepth = (typeof pathDepth === 'undefined' ? parsed.length : pathDepth);
|
||||
|
||||
for (var i = 0; i < pathDepth; i++) {
|
||||
var part = parsed[i];
|
||||
if (temporaryValue) {
|
||||
if (typeof part.p === 'undefined') {
|
||||
temporaryValue = temporaryValue[part.i];
|
||||
} else {
|
||||
temporaryValue = temporaryValue[part.p];
|
||||
}
|
||||
|
||||
if (i === (pathDepth - 1)) {
|
||||
res = temporaryValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* !
|
||||
* ## internalSetPathValue(obj, value, parsed)
|
||||
*
|
||||
* Companion function for `parsePath` that sets
|
||||
* the value located at a parsed address.
|
||||
*
|
||||
* internalSetPathValue(obj, 'value', parsed);
|
||||
*
|
||||
* @param {Object} object to search and define on
|
||||
* @param {*} value to use upon set
|
||||
* @param {Object} parsed definition from `parsePath`
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function internalSetPathValue(obj, val, parsed) {
|
||||
var tempObj = obj;
|
||||
var pathDepth = parsed.length;
|
||||
var part = null;
|
||||
// Here we iterate through every part of the path
|
||||
for (var i = 0; i < pathDepth; i++) {
|
||||
var propName = null;
|
||||
var propVal = null;
|
||||
part = parsed[i];
|
||||
|
||||
// If it's the last part of the path, we set the 'propName' value with the property name
|
||||
if (i === (pathDepth - 1)) {
|
||||
propName = typeof part.p === 'undefined' ? part.i : part.p;
|
||||
// Now we set the property with the name held by 'propName' on object with the desired val
|
||||
tempObj[propName] = val;
|
||||
} else if (typeof part.p !== 'undefined' && tempObj[part.p]) {
|
||||
tempObj = tempObj[part.p];
|
||||
} else if (typeof part.i !== 'undefined' && tempObj[part.i]) {
|
||||
tempObj = tempObj[part.i];
|
||||
} else {
|
||||
// If the obj doesn't have the property we create one with that name to define it
|
||||
var next = parsed[i + 1];
|
||||
// Here we set the name of the property which will be defined
|
||||
propName = typeof part.p === 'undefined' ? part.i : part.p;
|
||||
// Here we decide if this property will be an array or a new object
|
||||
propVal = typeof next.p === 'undefined' ? [] : {};
|
||||
tempObj[propName] = propVal;
|
||||
tempObj = tempObj[propName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ### .getPathInfo(object, path)
|
||||
*
|
||||
* This allows the retrieval of property info in an
|
||||
* object given a string path.
|
||||
*
|
||||
* The path info consists of an object with the
|
||||
* following properties:
|
||||
*
|
||||
* * parent - The parent object of the property referenced by `path`
|
||||
* * name - The name of the final property, a number if it was an array indexer
|
||||
* * value - The value of the property, if it exists, otherwise `undefined`
|
||||
* * exists - Whether the property exists or not
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String} path
|
||||
* @returns {Object} info
|
||||
* @namespace Utils
|
||||
* @name getPathInfo
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function getPathInfo(obj, path) {
|
||||
var parsed = parsePath(path);
|
||||
var last = parsed[parsed.length - 1];
|
||||
var info = {
|
||||
parent: parsed.length > 1 ? internalGetPathValue(obj, parsed, parsed.length - 1) : obj,
|
||||
name: last.p || last.i,
|
||||
value: internalGetPathValue(obj, parsed),
|
||||
};
|
||||
info.exists = hasProperty(info.parent, info.name);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* ### .getPathValue(object, path)
|
||||
*
|
||||
* This allows the retrieval of values in an
|
||||
* object given a string path.
|
||||
*
|
||||
* var obj = {
|
||||
* prop1: {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
* , prop2: {
|
||||
* arr: [ { nested: 'Universe' } ]
|
||||
* , str: 'Hello again!'
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* The following would be the results.
|
||||
*
|
||||
* getPathValue(obj, 'prop1.str'); // Hello
|
||||
* getPathValue(obj, 'prop1.att[2]'); // b
|
||||
* getPathValue(obj, 'prop2.arr[0].nested'); // Universe
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String} path
|
||||
* @returns {Object} value or `undefined`
|
||||
* @namespace Utils
|
||||
* @name getPathValue
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function getPathValue(obj, path) {
|
||||
var info = getPathInfo(obj, path);
|
||||
return info.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* ### .setPathValue(object, path, value)
|
||||
*
|
||||
* Define the value in an object at a given string path.
|
||||
*
|
||||
* ```js
|
||||
* var obj = {
|
||||
* prop1: {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
* , prop2: {
|
||||
* arr: [ { nested: 'Universe' } ]
|
||||
* , str: 'Hello again!'
|
||||
* }
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* The following would be acceptable.
|
||||
*
|
||||
* ```js
|
||||
* var properties = require('tea-properties');
|
||||
* properties.set(obj, 'prop1.str', 'Hello Universe!');
|
||||
* properties.set(obj, 'prop1.arr[2]', 'B');
|
||||
* properties.set(obj, 'prop2.arr[0].nested.value', { hello: 'universe' });
|
||||
* ```
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String} path
|
||||
* @param {Mixed} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function setPathValue(obj, path, val) {
|
||||
var parsed = parsePath(path);
|
||||
internalSetPathValue(obj, val, parsed);
|
||||
return obj;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hasProperty: hasProperty,
|
||||
getPathInfo: getPathInfo,
|
||||
getPathValue: getPathValue,
|
||||
setPathValue: setPathValue,
|
||||
};
|
110
node_modules/pathval/package.json
generated
vendored
Normal file
110
node_modules/pathval/package.json
generated
vendored
Normal file
@ -0,0 +1,110 @@
|
||||
{
|
||||
"_from": "pathval@^1.1.0",
|
||||
"_id": "pathval@1.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=",
|
||||
"_location": "/pathval",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "pathval@^1.1.0",
|
||||
"name": "pathval",
|
||||
"escapedName": "pathval",
|
||||
"rawSpec": "^1.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/chai"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz",
|
||||
"_shasum": "b942e6d4bde653005ef6b71361def8727d0645e0",
|
||||
"_spec": "pathval@^1.1.0",
|
||||
"_where": "/Users/josh.burman/Projects/braid/node_modules/chai",
|
||||
"author": {
|
||||
"name": "Veselin Todorov",
|
||||
"email": "hi@vesln.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/chaijs/pathval/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"config": {
|
||||
"ghooks": {
|
||||
"commit-msg": "validate-commit-msg"
|
||||
}
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Object value retrieval given a string path",
|
||||
"devDependencies": {
|
||||
"browserify": "^13.0.0",
|
||||
"browserify-istanbul": "^1.0.0",
|
||||
"coveralls": "2.11.9",
|
||||
"eslint": "^2.4.0",
|
||||
"eslint-config-strict": "^8.5.0",
|
||||
"eslint-plugin-filenames": "^0.2.0",
|
||||
"ghooks": "^1.0.1",
|
||||
"istanbul": "^0.4.2",
|
||||
"karma": "^0.13.22",
|
||||
"karma-browserify": "^5.0.2",
|
||||
"karma-coverage": "^0.5.5",
|
||||
"karma-mocha": "^0.2.2",
|
||||
"karma-phantomjs-launcher": "^1.0.0",
|
||||
"karma-sauce-launcher": "^0.3.1",
|
||||
"lcov-result-merger": "^1.0.2",
|
||||
"mocha": "^3.1.2",
|
||||
"phantomjs-prebuilt": "^2.1.5",
|
||||
"semantic-release": "^4.3.5",
|
||||
"simple-assert": "^1.0.0",
|
||||
"travis-after-all": "^1.4.4",
|
||||
"validate-commit-msg": "^2.3.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": [
|
||||
"strict/es5"
|
||||
],
|
||||
"env": {
|
||||
"es6": true
|
||||
},
|
||||
"globals": {
|
||||
"HTMLElement": false
|
||||
},
|
||||
"rules": {
|
||||
"complexity": 0,
|
||||
"max-statements": 0
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"pathval.js"
|
||||
],
|
||||
"homepage": "https://github.com/chaijs/pathval",
|
||||
"keywords": [
|
||||
"pathval",
|
||||
"value retrieval",
|
||||
"chai util"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "./index.js",
|
||||
"name": "pathval",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@github.com/chaijs/pathval.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "browserify --bare $npm_package_main --standalone pathval -o pathval.js",
|
||||
"lint": "eslint --ignore-path .gitignore .",
|
||||
"prepublish": "npm run build",
|
||||
"pretest": "npm run lint",
|
||||
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
|
||||
"test": "npm run test:node && npm run test:browser && npm run upload-coverage",
|
||||
"test:browser": "karma start --singleRun=true",
|
||||
"test:node": "istanbul cover _mocha",
|
||||
"upload-coverage": "lcov-result-merger 'coverage/**/lcov.info' | coveralls; exit 0"
|
||||
},
|
||||
"version": "1.1.0"
|
||||
}
|
295
node_modules/pathval/pathval.js
generated
vendored
Normal file
295
node_modules/pathval/pathval.js
generated
vendored
Normal file
@ -0,0 +1,295 @@
|
||||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pathval = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
||||
'use strict';
|
||||
|
||||
/* !
|
||||
* Chai - pathval utility
|
||||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
|
||||
* @see https://github.com/logicalparadox/filtr
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* ### .hasProperty(object, name)
|
||||
*
|
||||
* This allows checking whether an object has own
|
||||
* or inherited from prototype chain named property.
|
||||
*
|
||||
* Basically does the same thing as the `in`
|
||||
* operator but works properly with null/undefined values
|
||||
* and other primitives.
|
||||
*
|
||||
* var obj = {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
*
|
||||
* The following would be the results.
|
||||
*
|
||||
* hasProperty(obj, 'str'); // true
|
||||
* hasProperty(obj, 'constructor'); // true
|
||||
* hasProperty(obj, 'bar'); // false
|
||||
*
|
||||
* hasProperty(obj.str, 'length'); // true
|
||||
* hasProperty(obj.str, 1); // true
|
||||
* hasProperty(obj.str, 5); // false
|
||||
*
|
||||
* hasProperty(obj.arr, 'length'); // true
|
||||
* hasProperty(obj.arr, 2); // true
|
||||
* hasProperty(obj.arr, 3); // false
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String|Symbol} name
|
||||
* @returns {Boolean} whether it exists
|
||||
* @namespace Utils
|
||||
* @name hasProperty
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function hasProperty(obj, name) {
|
||||
if (typeof obj === 'undefined' || obj === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The `in` operator does not work with primitives.
|
||||
return name in Object(obj);
|
||||
}
|
||||
|
||||
/* !
|
||||
* ## parsePath(path)
|
||||
*
|
||||
* Helper function used to parse string object
|
||||
* paths. Use in conjunction with `internalGetPathValue`.
|
||||
*
|
||||
* var parsed = parsePath('myobject.property.subprop');
|
||||
*
|
||||
* ### Paths:
|
||||
*
|
||||
* * Can be infinitely deep and nested.
|
||||
* * Arrays are also valid using the formal `myobject.document[3].property`.
|
||||
* * Literal dots and brackets (not delimiter) must be backslash-escaped.
|
||||
*
|
||||
* @param {String} path
|
||||
* @returns {Object} parsed
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function parsePath(path) {
|
||||
var str = path.replace(/([^\\])\[/g, '$1.[');
|
||||
var parts = str.match(/(\\\.|[^.]+?)+/g);
|
||||
return parts.map(function mapMatches(value) {
|
||||
var regexp = /^\[(\d+)\]$/;
|
||||
var mArr = regexp.exec(value);
|
||||
var parsed = null;
|
||||
if (mArr) {
|
||||
parsed = { i: parseFloat(mArr[1]) };
|
||||
} else {
|
||||
parsed = { p: value.replace(/\\([.\[\]])/g, '$1') };
|
||||
}
|
||||
|
||||
return parsed;
|
||||
});
|
||||
}
|
||||
|
||||
/* !
|
||||
* ## internalGetPathValue(obj, parsed[, pathDepth])
|
||||
*
|
||||
* Helper companion function for `.parsePath` that returns
|
||||
* the value located at the parsed address.
|
||||
*
|
||||
* var value = getPathValue(obj, parsed);
|
||||
*
|
||||
* @param {Object} object to search against
|
||||
* @param {Object} parsed definition from `parsePath`.
|
||||
* @param {Number} depth (nesting level) of the property we want to retrieve
|
||||
* @returns {Object|Undefined} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function internalGetPathValue(obj, parsed, pathDepth) {
|
||||
var temporaryValue = obj;
|
||||
var res = null;
|
||||
pathDepth = (typeof pathDepth === 'undefined' ? parsed.length : pathDepth);
|
||||
|
||||
for (var i = 0; i < pathDepth; i++) {
|
||||
var part = parsed[i];
|
||||
if (temporaryValue) {
|
||||
if (typeof part.p === 'undefined') {
|
||||
temporaryValue = temporaryValue[part.i];
|
||||
} else {
|
||||
temporaryValue = temporaryValue[part.p];
|
||||
}
|
||||
|
||||
if (i === (pathDepth - 1)) {
|
||||
res = temporaryValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* !
|
||||
* ## internalSetPathValue(obj, value, parsed)
|
||||
*
|
||||
* Companion function for `parsePath` that sets
|
||||
* the value located at a parsed address.
|
||||
*
|
||||
* internalSetPathValue(obj, 'value', parsed);
|
||||
*
|
||||
* @param {Object} object to search and define on
|
||||
* @param {*} value to use upon set
|
||||
* @param {Object} parsed definition from `parsePath`
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function internalSetPathValue(obj, val, parsed) {
|
||||
var tempObj = obj;
|
||||
var pathDepth = parsed.length;
|
||||
var part = null;
|
||||
// Here we iterate through every part of the path
|
||||
for (var i = 0; i < pathDepth; i++) {
|
||||
var propName = null;
|
||||
var propVal = null;
|
||||
part = parsed[i];
|
||||
|
||||
// If it's the last part of the path, we set the 'propName' value with the property name
|
||||
if (i === (pathDepth - 1)) {
|
||||
propName = typeof part.p === 'undefined' ? part.i : part.p;
|
||||
// Now we set the property with the name held by 'propName' on object with the desired val
|
||||
tempObj[propName] = val;
|
||||
} else if (typeof part.p !== 'undefined' && tempObj[part.p]) {
|
||||
tempObj = tempObj[part.p];
|
||||
} else if (typeof part.i !== 'undefined' && tempObj[part.i]) {
|
||||
tempObj = tempObj[part.i];
|
||||
} else {
|
||||
// If the obj doesn't have the property we create one with that name to define it
|
||||
var next = parsed[i + 1];
|
||||
// Here we set the name of the property which will be defined
|
||||
propName = typeof part.p === 'undefined' ? part.i : part.p;
|
||||
// Here we decide if this property will be an array or a new object
|
||||
propVal = typeof next.p === 'undefined' ? [] : {};
|
||||
tempObj[propName] = propVal;
|
||||
tempObj = tempObj[propName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ### .getPathInfo(object, path)
|
||||
*
|
||||
* This allows the retrieval of property info in an
|
||||
* object given a string path.
|
||||
*
|
||||
* The path info consists of an object with the
|
||||
* following properties:
|
||||
*
|
||||
* * parent - The parent object of the property referenced by `path`
|
||||
* * name - The name of the final property, a number if it was an array indexer
|
||||
* * value - The value of the property, if it exists, otherwise `undefined`
|
||||
* * exists - Whether the property exists or not
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String} path
|
||||
* @returns {Object} info
|
||||
* @namespace Utils
|
||||
* @name getPathInfo
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function getPathInfo(obj, path) {
|
||||
var parsed = parsePath(path);
|
||||
var last = parsed[parsed.length - 1];
|
||||
var info = {
|
||||
parent: parsed.length > 1 ? internalGetPathValue(obj, parsed, parsed.length - 1) : obj,
|
||||
name: last.p || last.i,
|
||||
value: internalGetPathValue(obj, parsed),
|
||||
};
|
||||
info.exists = hasProperty(info.parent, info.name);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* ### .getPathValue(object, path)
|
||||
*
|
||||
* This allows the retrieval of values in an
|
||||
* object given a string path.
|
||||
*
|
||||
* var obj = {
|
||||
* prop1: {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
* , prop2: {
|
||||
* arr: [ { nested: 'Universe' } ]
|
||||
* , str: 'Hello again!'
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* The following would be the results.
|
||||
*
|
||||
* getPathValue(obj, 'prop1.str'); // Hello
|
||||
* getPathValue(obj, 'prop1.att[2]'); // b
|
||||
* getPathValue(obj, 'prop2.arr[0].nested'); // Universe
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String} path
|
||||
* @returns {Object} value or `undefined`
|
||||
* @namespace Utils
|
||||
* @name getPathValue
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function getPathValue(obj, path) {
|
||||
var info = getPathInfo(obj, path);
|
||||
return info.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* ### .setPathValue(object, path, value)
|
||||
*
|
||||
* Define the value in an object at a given string path.
|
||||
*
|
||||
* ```js
|
||||
* var obj = {
|
||||
* prop1: {
|
||||
* arr: ['a', 'b', 'c']
|
||||
* , str: 'Hello'
|
||||
* }
|
||||
* , prop2: {
|
||||
* arr: [ { nested: 'Universe' } ]
|
||||
* , str: 'Hello again!'
|
||||
* }
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* The following would be acceptable.
|
||||
*
|
||||
* ```js
|
||||
* var properties = require('tea-properties');
|
||||
* properties.set(obj, 'prop1.str', 'Hello Universe!');
|
||||
* properties.set(obj, 'prop1.arr[2]', 'B');
|
||||
* properties.set(obj, 'prop2.arr[0].nested.value', { hello: 'universe' });
|
||||
* ```
|
||||
*
|
||||
* @param {Object} object
|
||||
* @param {String} path
|
||||
* @param {Mixed} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function setPathValue(obj, path, val) {
|
||||
var parsed = parsePath(path);
|
||||
internalSetPathValue(obj, val, parsed);
|
||||
return obj;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hasProperty: hasProperty,
|
||||
getPathInfo: getPathInfo,
|
||||
getPathValue: getPathValue,
|
||||
setPathValue: setPathValue,
|
||||
};
|
||||
|
||||
},{}]},{},[1])(1)
|
||||
});
|
Reference in New Issue
Block a user