Comparing version 0.1.14 to 0.1.15
@@ -1,2 +0,2 @@ | ||
import { Segments, Node, Pattern, MatchInterceptor } from './types'; | ||
import { Segments, Node, Pattern } from './types'; | ||
export * from './types'; | ||
@@ -29,3 +29,3 @@ export declare class Path { | ||
transform: <T>(regexp: string | RegExp, callback: (...args: string[]) => T) => string | T; | ||
match: (pattern: Pattern, interceptor?: MatchInterceptor) => boolean; | ||
match: (pattern: Pattern) => boolean; | ||
existIn: (source?: any, start?: number | Path) => boolean; | ||
@@ -35,3 +35,3 @@ getIn: (source?: any) => any; | ||
deleteIn: (source?: any) => any; | ||
static match(pattern: Pattern, interceptor?: MatchInterceptor): { | ||
static match(pattern: Pattern): { | ||
(target: any): boolean; | ||
@@ -38,0 +38,0 @@ path: Path; |
@@ -252,10 +252,9 @@ "use strict"; | ||
}; | ||
this.match = function (pattern, interceptor) { | ||
this.match = function (pattern) { | ||
var path = Path.getPath(pattern); | ||
var cache = _this.matchCache.get(path.entire); | ||
if (cache !== undefined && !interceptor) | ||
if (cache !== undefined) | ||
return cache; | ||
var cacheWith = function (value) { | ||
if (!interceptor) | ||
_this.matchCache.set(path.entire, value); | ||
_this.matchCache.set(path.entire, value); | ||
return value; | ||
@@ -268,3 +267,3 @@ }; | ||
else { | ||
return cacheWith(path.match(_this.segments, interceptor)); | ||
return cacheWith(path.match(_this.segments)); | ||
} | ||
@@ -274,6 +273,6 @@ } | ||
if (_this.isMatchPattern) { | ||
return cacheWith(new matcher_1.Matcher(_this.tree, interceptor).match(path.segments)); | ||
return cacheWith(new matcher_1.Matcher(_this.tree).match(path.segments)); | ||
} | ||
else { | ||
return cacheWith(matcher_1.Matcher.matchSegments(_this.segments, path.segments, interceptor)); | ||
return cacheWith(matcher_1.Matcher.matchSegments(_this.segments, path.segments)); | ||
} | ||
@@ -392,6 +391,6 @@ } | ||
}; | ||
Path.match = function (pattern, interceptor) { | ||
Path.match = function (pattern) { | ||
var path = Path.getPath(pattern); | ||
var matcher = function (target) { | ||
return path.match(target, interceptor); | ||
return path.match(target); | ||
}; | ||
@@ -398,0 +397,0 @@ matcher.path = path; |
@@ -1,2 +0,2 @@ | ||
import { Segments, Node, IdentifierNode, IgnoreExpressionNode, DestructorExpressionNode, ExpandOperatorNode, WildcardOperatorNode, GroupExpressionNode, RangeExpressionNode, DotOperatorNode, MatchInterceptor } from './types'; | ||
import { Segments, Node, IdentifierNode, IgnoreExpressionNode, DestructorExpressionNode, ExpandOperatorNode, WildcardOperatorNode, GroupExpressionNode, RangeExpressionNode, DotOperatorNode } from './types'; | ||
export declare class Matcher { | ||
@@ -7,7 +7,6 @@ private tree; | ||
private stack; | ||
private interceptor; | ||
constructor(tree: Node, interceptor?: MatchInterceptor); | ||
constructor(tree: Node); | ||
currentElement(path: Segments): string; | ||
matchNext: (node: any, path: any) => any; | ||
matchIdentifier(path: Segments, node: IdentifierNode): boolean; | ||
matchIdentifier(path: Segments, node: IdentifierNode): any; | ||
matchIgnoreExpression(path: Segments, node: IgnoreExpressionNode): any; | ||
@@ -22,3 +21,3 @@ matchDestructorExpression(path: Segments, node: DestructorExpressionNode): any; | ||
match(path: Segments): any; | ||
static matchSegments(source: Segments, target: Segments, interceptor?: MatchInterceptor): any; | ||
static matchSegments(source: Segments, target: Segments): any; | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
var Matcher = (function () { | ||
function Matcher(tree, interceptor) { | ||
function Matcher(tree) { | ||
var _this = this; | ||
@@ -15,3 +15,2 @@ this.matchNext = function (node, path) { | ||
this.stack = []; | ||
this.interceptor = interceptor; | ||
} | ||
@@ -46,10 +45,2 @@ Matcher.prototype.currentElement = function (path) { | ||
} | ||
if (this.interceptor) { | ||
return this.interceptor({ | ||
path: path.slice(0, this.pos + 1), | ||
source: path, | ||
current: current, | ||
next: next | ||
}); | ||
} | ||
return current() && next(); | ||
@@ -171,3 +162,3 @@ }; | ||
}; | ||
Matcher.matchSegments = function (source, target, interceptor) { | ||
Matcher.matchSegments = function (source, target) { | ||
var pos = 0; | ||
@@ -179,10 +170,2 @@ if (source.length !== target.length) | ||
var next = function () { return (pos < source.length - 1 ? match(pos + 1) : true); }; | ||
if (interceptor) { | ||
return interceptor({ | ||
path: target.slice(0, pos + 1), | ||
source: target, | ||
current: current, | ||
next: next | ||
}); | ||
} | ||
return current() && next(); | ||
@@ -189,0 +172,0 @@ }; |
@@ -73,9 +73,2 @@ import { Path } from './index'; | ||
export declare const isArrayPattern: (obj: any) => obj is ArrayPatternNode; | ||
export declare type MatchAPI = { | ||
path: Segments; | ||
source: Segments; | ||
current: () => boolean; | ||
next: () => boolean; | ||
}; | ||
export declare type MatchInterceptor = (match: MatchAPI) => boolean; | ||
export {}; |
{ | ||
"name": "cool-path", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "Path Matcher/Getter/Setter for Object/Array", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -10,3 +10,3 @@ import { Parser } from './parser' | ||
} from './destructor' | ||
import { Segments, Node, Pattern, MatchInterceptor } from './types' | ||
import { Segments, Node, Pattern } from './types' | ||
export * from './types' | ||
@@ -362,8 +362,8 @@ import { LRUMap } from './lru' | ||
match = (pattern: Pattern, interceptor?: MatchInterceptor): boolean => { | ||
match = (pattern: Pattern): boolean => { | ||
const path = Path.getPath(pattern) | ||
const cache = this.matchCache.get(path.entire) | ||
if (cache !== undefined && !interceptor) return cache | ||
if (cache !== undefined) return cache | ||
const cacheWith = (value: boolean): boolean => { | ||
if (!interceptor) this.matchCache.set(path.entire, value) | ||
this.matchCache.set(path.entire, value) | ||
return value | ||
@@ -375,13 +375,9 @@ } | ||
} else { | ||
return cacheWith(path.match(this.segments, interceptor)) | ||
return cacheWith(path.match(this.segments)) | ||
} | ||
} else { | ||
if (this.isMatchPattern) { | ||
return cacheWith( | ||
new Matcher(this.tree, interceptor).match(path.segments) | ||
) | ||
return cacheWith(new Matcher(this.tree).match(path.segments)) | ||
} else { | ||
return cacheWith( | ||
Matcher.matchSegments(this.segments, path.segments, interceptor) | ||
) | ||
return cacheWith(Matcher.matchSegments(this.segments, path.segments)) | ||
} | ||
@@ -409,6 +405,6 @@ } | ||
static match(pattern: Pattern, interceptor?: MatchInterceptor) { | ||
static match(pattern: Pattern) { | ||
const path = Path.getPath(pattern) | ||
const matcher = target => { | ||
return path.match(target, interceptor) | ||
return path.match(target) | ||
} | ||
@@ -415,0 +411,0 @@ matcher.path = path |
@@ -19,5 +19,3 @@ import { | ||
RangeExpressionNode, | ||
DotOperatorNode, | ||
MatchInterceptor, | ||
MatchAPI | ||
DotOperatorNode | ||
} from './types' | ||
@@ -35,9 +33,6 @@ import { isEqual, toArray } from './utils' | ||
private interceptor: MatchInterceptor | ||
constructor(tree: Node, interceptor?: MatchInterceptor) { | ||
constructor(tree: Node) { | ||
this.tree = tree | ||
this.pos = 0 | ||
this.stack = [] | ||
this.interceptor = interceptor | ||
} | ||
@@ -64,3 +59,3 @@ | ||
} | ||
let current: MatchAPI['current'], next: MatchAPI['next'] | ||
let current: any, next: any | ||
@@ -76,11 +71,2 @@ if (isExpandOperator(node.after)) { | ||
if (this.interceptor) { | ||
return this.interceptor({ | ||
path: path.slice(0, this.pos + 1), | ||
source: path, | ||
current, | ||
next | ||
}) | ||
} | ||
return current() && next() | ||
@@ -209,4 +195,3 @@ } | ||
source: Segments, | ||
target: Segments, | ||
interceptor?: MatchInterceptor | ||
target: Segments | ||
) { | ||
@@ -218,10 +203,2 @@ let pos = 0 | ||
const next = () => (pos < source.length - 1 ? match(pos + 1) : true) | ||
if (interceptor) { | ||
return interceptor({ | ||
path: target.slice(0, pos + 1), | ||
source: target, | ||
current, | ||
next | ||
}) | ||
} | ||
return current() && next() | ||
@@ -228,0 +205,0 @@ } |
@@ -121,10 +121,1 @@ import { Path } from './index' | ||
export const isArrayPattern = isType<ArrayPatternNode>('ArrayPattern') | ||
export type MatchAPI = { | ||
path: Segments | ||
source: Segments | ||
current: () => boolean | ||
next: () => boolean | ||
} | ||
export type MatchInterceptor = (match: MatchAPI) => boolean |
@@ -34,20 +34,2 @@ import expect from 'expect' | ||
test('interceptor match', () => { | ||
expect(new Path('aa.bb.cc').match(['aa', 'kk', 'cc'])).toEqual(false) | ||
expect( | ||
new Path('aa.bb.cc').match( | ||
['oo', 'kk', 'cc'], | ||
({ path, source, current, next }) => { | ||
console.log(path, source) | ||
const last = path[path.length - 1] | ||
if (last == 'kk' || last == 'oo') { | ||
return next() | ||
} else { | ||
return current() && next() | ||
} | ||
} | ||
) | ||
).toEqual(true) | ||
}) | ||
match({ | ||
@@ -54,0 +36,0 @@ '*': [[], ['aa'], ['aa', 'bb', 'cc'], ['aa', 'dd', 'gg']], |
157004
5020