Skip to content

Commit

Permalink
chore(all): prepare release 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Jun 14, 2018
1 parent a50adc2 commit cf03be4
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 253 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aurelia-route-recognizer",
"version": "1.1.1",
"version": "1.2.0",
"description": "A lightweight JavaScript library that matches paths against registered routes. It includes support for dynamic and star segments and nested handlers.",
"keywords": [
"aurelia",
Expand Down
79 changes: 34 additions & 45 deletions dist/amd/aurelia-route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
};

DynamicSegment.prototype.regex = function regex() {
return this.optional ? '([^/]+)?' : '([^/]+)';
return '([^/]+)';
};

DynamicSegment.prototype.generate = function generate(params, consumed) {
Expand Down Expand Up @@ -199,12 +199,12 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
}

var currentState = this.rootState;
var skippableStates = [];
var regex = '^';
var types = { statics: 0, dynamics: 0, stars: 0 };
var names = [];
var routeName = route.handler.name;
var isEmpty = true;
var isAllOptional = true;
var segments = parse(route.path, names, types, route.caseSensitive);

for (var i = 0, ii = segments.length; i < ii; i++) {
Expand All @@ -213,25 +213,30 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
continue;
}

isEmpty = false;
isAllOptional = isAllOptional && segment.optional;
var _addSegment = addSegment(currentState, segment),
firstState = _addSegment[0],
nextState = _addSegment[1];

currentState = addSegment(currentState, segment);
regex += segment.optional ? '/?' : '/';
regex += segment.regex();
}
for (var j = 0, jj = skippableStates.length; j < jj; j++) {
skippableStates[j].nextStates.push(firstState);
}

if (isAllOptional) {
if (isEmpty) {
currentState = currentState.put({ validChars: '/' });
regex += '/';
if (segment.optional) {
skippableStates.push(nextState);
regex += '(?:/' + segment.regex() + ')?';
} else {
var finalState = this.rootState.put({ validChars: '/' });
currentState.epsilon = [finalState];
currentState = finalState;
currentState = nextState;
regex += '/' + segment.regex();
skippableStates.length = 0;
isEmpty = false;
}
}

if (isEmpty) {
currentState = currentState.put({ validChars: '/' });
regex += '/?';
}

var handlers = [{ handler: route.handler, names: names }];

if (routeName) {
Expand All @@ -244,6 +249,13 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
}
}

for (var _i3 = 0; _i3 < skippableStates.length; _i3++) {
var state = skippableStates[_i3];
state.handlers = handlers;
state.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
state.types = types;
}

currentState.handlers = handlers;
currentState.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
currentState.types = types;
Expand Down Expand Up @@ -345,9 +357,9 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
}

var solutions = [];
for (var _i3 = 0, _l = states.length; _i3 < _l; _i3++) {
if (states[_i3].handlers) {
solutions.push(states[_i3]);
for (var _i4 = 0, _l = states.length; _i4 < _l; _i4++) {
if (states[_i4].handlers) {
solutions.push(states[_i4]);
}
}

Expand Down Expand Up @@ -454,25 +466,6 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
nextStates.push.apply(nextStates, state.match(ch));
}

var skippableStates = nextStates.filter(function (s) {
return s.epsilon;
});

var _loop = function _loop() {
var newStates = [];
skippableStates.forEach(function (s) {
nextStates.push.apply(nextStates, s.epsilon);
newStates.push.apply(newStates, s.epsilon);
});
skippableStates = newStates.filter(function (s) {
return s.epsilon;
});
};

while (skippableStates.length > 0) {
_loop();
}

return nextStates;
}

Expand All @@ -499,16 +492,12 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
}

function addSegment(currentState, segment) {
var state = currentState.put({ validChars: '/' });
var firstState = currentState.put({ validChars: '/' });
var nextState = firstState;
segment.eachChar(function (ch) {
state = state.put(ch);
nextState = nextState.put(ch);
});

if (segment.optional) {
currentState.epsilon = currentState.epsilon || [];
currentState.epsilon.push(state);
}

return state;
return [firstState, nextState];
}
});
2 changes: 1 addition & 1 deletion dist/aurelia-route-recognizer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ export declare class RouteRecognizer {
* `isDynanic` values for the matched route(s), or undefined if no match
* was found.
*/
recognize(path: string): RecognizedRoute[] | undefined;
recognize(path: string): RecognizedRoute[] | void;
}
78 changes: 42 additions & 36 deletions dist/aurelia-route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class DynamicSegment {
}

regex(): string {
return this.optional ? '([^/]+)?' : '([^/]+)';
return '([^/]+)';
}

generate(params: Object, consumed: Object): string {
Expand Down Expand Up @@ -236,12 +236,12 @@ export class RouteRecognizer {
}

let currentState = this.rootState;
let skippableStates = [];
let regex = '^';
let types = { statics: 0, dynamics: 0, stars: 0 };
let names = [];
let routeName = route.handler.name;
let isEmpty = true;
let isAllOptional = true;
let segments = parse(route.path, names, types, route.caseSensitive);

for (let i = 0, ii = segments.length; i < ii; i++) {
Expand All @@ -250,27 +250,38 @@ export class RouteRecognizer {
continue;
}

isEmpty = false;
isAllOptional = isAllOptional && segment.optional;

// Add a representation of the segment to the NFA and regex
currentState = addSegment(currentState, segment);
regex += segment.optional ? '/?' : '/';
regex += segment.regex();
}
let [firstState, nextState] = addSegment(currentState, segment);

// add the first part of the next segment to the end of any skipped states
for (let j = 0, jj = skippableStates.length; j < jj; j++) {
skippableStates[j].nextStates.push(firstState);
}

if (isAllOptional) {
if (isEmpty) {
currentState = currentState.put({ validChars: '/' });
regex += '/';
// If the segment was optional we don't fast forward to the end of the
// segment, but we do hold on to a reference to the end of the segment
// for adding future segments. Multiple consecutive optional segments
// will accumulate.
if (segment.optional) {
skippableStates.push(nextState);
regex += `(?:/${segment.regex()})?`;

// Otherwise, we fast forward to the end of the segment and remove any
// references to skipped segments since we don't need them anymore.
} else {
let finalState = this.rootState.put({ validChars: '/' });
currentState.epsilon = [ finalState ];
currentState = finalState;
// Regex is ok because the first '/?' will match.
currentState = nextState;
regex += `/${segment.regex()}`;
skippableStates.length = 0;
isEmpty = false;
}
}
// An "all optional" path is technically empty since currentState is this.rootState
if (isEmpty) {
currentState = currentState.put({ validChars: '/' });
regex += '/?';
}
let handlers = [{ handler: route.handler, names: names }];
if (routeName) {
Expand All @@ -283,6 +294,15 @@ export class RouteRecognizer {
}
}
// Any trailing skipped states need to be endpoints and need to have
// handlers attached.
for (let i = 0; i < skippableStates.length; i++) {
let state = skippableStates[i];
state.handlers = handlers;
state.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
state.types = types;
}
currentState.handlers = handlers;
currentState.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
currentState.types = types;
Expand Down Expand Up @@ -380,7 +400,7 @@ export class RouteRecognizer {
* `isDynanic` values for the matched route(s), or undefined if no match
* was found.
*/
recognize(path: string): RecognizedRoute[] | undefined {
recognize(path: string): RecognizedRoute[] | void {
let states = [this.rootState];
let queryParams = {};
let isSlashDropped = false;
Expand Down Expand Up @@ -533,16 +553,6 @@ function recognizeChar(states, ch) {
nextStates.push(...state.match(ch));
}

let skippableStates = nextStates.filter(s => s.epsilon);
while (skippableStates.length > 0) {
let newStates = [];
skippableStates.forEach(s => {
nextStates.push(...s.epsilon);
newStates.push(...s.epsilon);
});
skippableStates = newStates.filter(s => s.epsilon);
}

return nextStates;
}

Expand All @@ -569,15 +579,11 @@ function findHandler(state, path, queryParams) {
}

function addSegment(currentState, segment) {
let state = currentState.put({ validChars: '/' });
let firstState = currentState.put({ validChars: '/' });
let nextState = firstState;
segment.eachChar(ch => {
state = state.put(ch);
nextState = nextState.put(ch);
});

if (segment.optional) {
currentState.epsilon = currentState.epsilon || [];
currentState.epsilon.push(state);
}

return state;
return [firstState, nextState];
}
Loading

0 comments on commit cf03be4

Please sign in to comment.