js-pointer
Advanced tools
Comparing version 1.0.1 to 2.0.0
'use strict'; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
module.exports = { | ||
@@ -19,11 +25,12 @@ get: get | ||
function parse(pointer) { | ||
if (pointer === '') return []; | ||
validatePointer(pointer); | ||
return normaliseFragment(pointer).split('/').slice(1).map(unescape); | ||
} | ||
if (!(pointer[0] === '/')) { | ||
var err = new Error('Non-empty pointer must start with "/"'); | ||
err.name = 'InvalidPointerError'; | ||
throw err; | ||
} | ||
function normaliseFragment(pointer) { | ||
return isURIFragment(pointer) ? decodeURIComponent(pointer).slice(1) : pointer; | ||
} | ||
return pointer.split('/').slice(1).map(unescape); | ||
function isURIFragment(string) { | ||
return string && string[0] === '#'; | ||
} | ||
@@ -33,2 +40,23 @@ | ||
return token.replace('~1', '/').replace('~0', '~'); | ||
} | ||
} | ||
function validatePointer(pointer) { | ||
if (!/^([#\/]?$|#?\/.*)/.test(pointer)) { | ||
throw new InvalidPointerError(pointer); | ||
} | ||
} | ||
var InvalidPointerError = function (_Error) { | ||
_inherits(InvalidPointerError, _Error); | ||
function InvalidPointerError(pointer) { | ||
_classCallCheck(this, InvalidPointerError); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(InvalidPointerError).call(this, 'Pointer "' + pointer + '" is invalid')); | ||
_this.name = 'InvalidPointerError'; | ||
return _this; | ||
} | ||
return InvalidPointerError; | ||
}(Error); |
{ | ||
"name": "js-pointer", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Tiny, spec compliant JSON Pointer RFC 6901 implementation.", | ||
@@ -10,3 +10,3 @@ "main": "dist/index.js", | ||
"coverage": "nyc --require babel-core/register --reporter=lcov mocha ./test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"lint": "standard", | ||
"lint": "eslint 'lib/**' 'test/**'", | ||
"test": "nyc --check-coverage --require babel-core/register mocha ./test", | ||
@@ -42,6 +42,6 @@ "test-dist": "TEST_TARGET=dist mocha --require babel-core/register", | ||
"coveralls": "^2.11.9", | ||
"eslint": "^2.9.0", | ||
"lodash": "^4.8.2", | ||
"mocha": "^2.4.5", | ||
"nyc": "^6.4.0", | ||
"standard": "^6.0.8" | ||
"nyc": "^6.4.0" | ||
}, | ||
@@ -48,0 +48,0 @@ "standard": { |
# js-pointer | ||
[![Build Status](https://travis-ci.org/toboid/js-pointer.svg?branch=master)](https://travis-ci.org/toboid/js-pointer) | ||
[![Coverage Status](https://coveralls.io/repos/github/toboid/js-pointer/badge.svg?branch=master)](https://coveralls.io/github/toboid/js-pointer?branch=master) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) | ||
[![Dependencies](https://david-dm.org/toboid/js-pointer.svg)](https://github.com/toboid/js-pointer/blob/master/package.json) | ||
@@ -16,20 +15,34 @@ [![npm version](https://badge.fury.io/js/js-pointer.svg)](https://badge.fury.io/js/js-pointer) | ||
### Point to object properties | ||
``` javascript | ||
var jsPointer = require('js-pointer'); | ||
var object = { one: { two: { three: [{ four: 4 }] } } } | ||
jsonPointer.get(object, '/one/two/three/0/four') // returns 4 | ||
var object = { one: { two: 3 } } | ||
jsonPointer.get(object, '/one/two') // returns 3 | ||
``` | ||
### Pointing to array elements | ||
``` javascript | ||
var object = { one: { two: [3] } } | ||
jsonPointer.get(object, '/one/two/0') // returns 3 | ||
``` | ||
Please see the [spec](https://tools.ietf.org/html/rfc6901) and [tests](https://github.com/toboid/js-pointer/blob/master/test/dereferencing-tests.js) for further detail of the json pointer format. | ||
### Pointing through array elements | ||
``` javascript | ||
var object = { one: { two: [{ three: 4 }] } } | ||
jsonPointer.get(object, '/one/two/0/three') // returns 4 | ||
``` | ||
Please see the [spec](https://tools.ietf.org/html/rfc6901) and [tests](https://github.com/toboid/js-pointer/blob/master/test/dereferencing-tests.js) for further detail of the JSON pointer format. | ||
## API | ||
`jsPointer.get(object, pointer)` | ||
returns sub-object or value in `object` referred to by `pointer` | ||
Returns sub-object or value in `object` referred to by `pointer`. | ||
If `pointer` does not refer to an object or value then `undefined` will be returned. | ||
### object | ||
Plain object targeted by the pointer | ||
Plain **object** targeted by the pointer | ||
### pointer | ||
**string** JSON pointer | ||
**string** JSON pointer. A pointer beginning with `#` indicates a URI fragment, which will be URI decoded before processing. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6778
5
43
48