From bfc67bab9f8aaee8e77c0b6c6ef7594d6567139e Mon Sep 17 00:00:00 2001 From: Brendan Abbott Date: Tue, 15 Aug 2017 19:08:37 +1000 Subject: [PATCH 1/2] Run benchmarks over the built source --- README.md | 8 +++----- benchmarks/dereference-address-schema.benchmark.ts | 2 +- benchmarks/dereference-petstore-swagger.benchmark.ts | 2 +- benchmarks/dereference-temando-swagger.benchmark.ts | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2f4659c..ac0bebd 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ const data = { foo: 99 }; -set(data, '#/foo', 77); +set(data, '#/foo', 77); console.log(data); // { foo: 77 } ``` @@ -146,7 +146,7 @@ development. ### Requirements -- nodeJS >= 6 +- NodeJS >= 6 ### Setup @@ -157,9 +157,7 @@ you can find more in `package.json`. - `npm build`: build all source code and documentation - `npm test`: run all unit test. -- `npm benchmark`: run the benchmark suite. +- `npm benchmark`: run the benchmark suite (requires `npm build` to have been run) Make your changes and commit your code. When you are ready, send a pull request to this repository. - - diff --git a/benchmarks/dereference-address-schema.benchmark.ts b/benchmarks/dereference-address-schema.benchmark.ts index f39500d..2dfcdfe 100755 --- a/benchmarks/dereference-address-schema.benchmark.ts +++ b/benchmarks/dereference-address-schema.benchmark.ts @@ -1,5 +1,5 @@ import * as derefSync from 'json-schema-deref-sync'; -import { dereference } from './../src/index'; +import { dereference } from './../dist/index'; import resolve from './../tests/mockResolve'; import benchmark from './benchmark'; diff --git a/benchmarks/dereference-petstore-swagger.benchmark.ts b/benchmarks/dereference-petstore-swagger.benchmark.ts index 965a0bc..15532bb 100755 --- a/benchmarks/dereference-petstore-swagger.benchmark.ts +++ b/benchmarks/dereference-petstore-swagger.benchmark.ts @@ -1,6 +1,6 @@ import * as derefSync from 'json-schema-deref-sync'; import * as $RefParser from 'json-schema-ref-parser'; -import { dereference } from './../src/index'; +import { dereference } from './../dist/index'; import resolve from './../tests/mockResolve'; import benchmark from './benchmark'; diff --git a/benchmarks/dereference-temando-swagger.benchmark.ts b/benchmarks/dereference-temando-swagger.benchmark.ts index 275e1e6..bb7da5f 100755 --- a/benchmarks/dereference-temando-swagger.benchmark.ts +++ b/benchmarks/dereference-temando-swagger.benchmark.ts @@ -1,6 +1,6 @@ import * as derefSync from 'json-schema-deref-sync'; import * as $RefParser from 'json-schema-ref-parser'; -import { dereference } from './../src/index'; +import { dereference } from './../dist/index'; import resolve from './../tests/mockResolve'; import benchmark from './benchmark'; From 02faef9f9fb4bdb1d07b3612be9619423bf4f8cf Mon Sep 17 00:00:00 2001 From: Brendan Abbott Date: Tue, 15 Aug 2017 20:20:27 +1000 Subject: [PATCH 2/2] Use isPointer internally, now with a faster test --- src/get.ts | 3 ++- src/isPointer.ts | 3 ++- src/set.ts | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/get.ts b/src/get.ts index 41645c1..131918c 100644 --- a/src/get.ts +++ b/src/get.ts @@ -26,6 +26,7 @@ // ## Dependencies import * as has from 'lodash.has'; +import { isPointer } from './isPointer'; // #### Implementation @@ -35,7 +36,7 @@ export const get: Jst.getPointer = (schema, pointer) => { // A JSON `pointer` must begin with the symbols '#', '/' or be an empty // string ''. So as a first step, we check that this assumption is true and // bail if not. - if (!(pointer.indexOf('#') === 0 || pointer.indexOf('/') === 0 || pointer === '')) { + if (!isPointer(pointer)) { throw new Error(`invalid JSON pointer specified: '${pointer}'`); } diff --git a/src/isPointer.ts b/src/isPointer.ts index f192b31..b926c7d 100644 --- a/src/isPointer.ts +++ b/src/isPointer.ts @@ -1,3 +1,4 @@ +// A JSON `pointer` must begin with the symbols '#', '/' or be an empty string ''. export const isPointer = (input: string): boolean => { if (typeof input !== 'string') { return false; @@ -7,7 +8,7 @@ export const isPointer = (input: string): boolean => { return true; } - if (input.match(/^#|^\//) !== null) { + if (/^#|^\//.test(input)) { return true; } diff --git a/src/set.ts b/src/set.ts index 1596d4a..bb7996d 100644 --- a/src/set.ts +++ b/src/set.ts @@ -1,11 +1,12 @@ import * as has from 'lodash.has'; +import { isPointer } from './isPointer'; export const set: Jst.setPointer = (obj, pointer, value) => { // A JSON `pointer` must begin with the symbols '#', '/' or be an empty // string ''. So as a first step, we check that this assumption is true and // bail if not. - if (!(pointer.indexOf('#') === 0 || pointer.indexOf('/') === 0 || pointer === '')) { + if (!isPointer(pointer)) { throw new Error(`invalid JSON pointer specified: '${pointer}'`); }