Skip to content

Commit 6378331

Browse files
committed
feat: add buildState function
1 parent 142476f commit 6378331

3 files changed

Lines changed: 51 additions & 12 deletions

File tree

modules/RouteNode.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,8 @@ export default class RouteNode {
203203
return segments.map(segment => segment.parser.build(params, {ignoreSearch: true})).join('') + (searchPart ? '?' + searchPart : '')
204204
}
205205

206-
buildPath(routeName, params = {}) {
207-
return this.buildPathFromSegments(this.getSegmentsByName(routeName), params)
208-
}
209-
210-
getMatchPathFromSegments(segments) {
211-
if (!segments || !segments.length) return null
212-
213-
const _meta = segments.reduce((meta, segment) => {
206+
getMetaFromSegments(segments) {
207+
return segments.reduce((meta, segment) => {
214208
const urlParams = segment.parser.urlParams.reduce((params, p) => {
215209
params[p] = 'url';
216210
return params;
@@ -224,14 +218,37 @@ export default class RouteNode {
224218
meta[segment.name] = allParams;
225219
return meta;
226220
}, {});
221+
}
222+
223+
buildPath(routeName, params = {}) {
224+
return this.buildPathFromSegments(this.getSegmentsByName(routeName), params)
225+
}
226+
227+
buildStateFromSegments(segments) {
228+
if (!segments || !segments.length) return null;
229+
230+
const name = segments.map(segment => segment.name).join('.');
231+
const params = segments.params;
232+
233+
return {
234+
name,
235+
params,
236+
_meta: this.getMetaFromSegments(segments)
237+
};
238+
}
227239

228-
const name = segments.map(segment => segment.name).join('.')
229-
const params = segments.params
240+
buildState(name, params = {}) {
241+
const segments = this.getSegmentsByName(name);
242+
if (!segments || !segments.length) return null;
230243

231-
return {name, params, _meta}
244+
return {
245+
name,
246+
params,
247+
_meta: this.getMetaFromSegments(segments)
248+
};
232249
}
233250

234251
matchPath(path, trailingSlash = false) {
235-
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path, trailingSlash))
252+
return this.buildStateFromSegments(this.getSegmentsMatchingPath(path, trailingSlash))
236253
}
237254
}

scripts/build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ async.parallel([
2020
buildFactory('amd', 'dist/amd/route-node.js'),
2121
buildFactory('umd', 'dist/umd/route-node.js')
2222
], function (err) {
23+
if (err) console.log(err);
2324
process.exit(err ? 1 : 0);
2425
})

test/main.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ describe('RouteNode', function () {
119119
}).should.throw();
120120
});
121121

122+
it('should build the state object of a nested route', function () {
123+
var node = getRoutes();
124+
// Building paths
125+
node.buildState('home').should.eql({
126+
_meta: { home: {} },
127+
name: 'home',
128+
params: {}
129+
});
130+
131+
node.buildState('users.view', {id: 1}).should.eql({
132+
_meta: {
133+
users: {},
134+
view: {
135+
id: 'url'
136+
}
137+
},
138+
name: 'users.view',
139+
params: {id: 1}
140+
});
141+
});
142+
122143
it('should find a nested route by matching a path', function () {
123144
var node = getRoutes();
124145
// Building paths

0 commit comments

Comments
 (0)