Comparing version
56
index.js
@@ -135,27 +135,4 @@ var Map = require('immutable').Map; | ||
}; | ||
ParamTrie.ofPath = function (a0, a1) { | ||
if (Array.isArray ? Array.isArray(a0) : Object.prototype.toString.call(a0) === '[object Array]') { | ||
if (a0.length === 0) { | ||
var v = a1; | ||
return ParamTrie.of(v); | ||
} | ||
if (a0.length >= 1) { | ||
var r0 = []; | ||
var r1 = 1; | ||
for (var r2 = 1, r3 = a0.length, r4; r2 < r3; r2++) { | ||
r4 = a0[r2]; | ||
r0[r0.length] = r4; | ||
} | ||
if (r1) { | ||
var b = a0[0]; | ||
var rest = r0; | ||
var v = a1; | ||
return new ParamTrie([], Map([[ | ||
b, | ||
ParamTrie.ofPath(rest, v) | ||
]])); | ||
} | ||
} | ||
} | ||
throw new TypeError('No match'); | ||
ParamTrie.ofPath = function (p, v) { | ||
return ParamTrie.of(v).indent(p); | ||
}; | ||
@@ -266,4 +243,33 @@ ParamTrie.fromMap = function (m) { | ||
}; | ||
function indent(a0, a1) { | ||
if ((ParamTrie.hasInstance ? ParamTrie.hasInstance(a0) : a0 instanceof ParamTrie) && (Array.isArray ? Array.isArray(a1) : Object.prototype.toString.call(a1) === '[object Array]')) { | ||
if (a1.length === 0) { | ||
var p = a0; | ||
return p; | ||
} | ||
if (a1.length >= 1) { | ||
var r0 = []; | ||
var r1 = 1; | ||
for (var r2 = 1, r3 = a1.length, r4; r2 < r3; r2++) { | ||
r4 = a1[r2]; | ||
r0[r0.length] = r4; | ||
} | ||
if (r1) { | ||
var p = a0; | ||
var b = a1[0]; | ||
var rest = r0; | ||
return new ParamTrie([], Map([[ | ||
b, | ||
indent(p, rest) | ||
]])); | ||
} | ||
} | ||
} | ||
throw new TypeError('No match'); | ||
} | ||
ParamTrie.prototype.indent = function (p) { | ||
return indent(this, p); | ||
}; | ||
exports.ParamTrie = ParamTrie; | ||
exports.ParamBranch = ParamBranch; | ||
exports.LookupResult = LookupResult; |
{ | ||
"name": "param-trie", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A trie with holes for parameters", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
<h1 align="center"> | ||
param-trie<br> | ||
<code>npm install param-trie</code><br> | ||
<a href="https://travis-ci.org/quarterto/ParamTrie" id="status-image-popup"> | ||
@@ -86,3 +86,7 @@ <img src="https://travis-ci.org/quarterto/ParamTrie.svg" alt="Build status"> | ||
#### `ParamTrie<T>::indent(path: Array<ParamBranch>)` | ||
Returns the trie nested under the path. `ParamTrie.ofPath(p, v)` is equivalent to `ParamTrie.of(v).indent(p)`. | ||
## Licence | ||
MIT. | ||
MIT. |
41
test.js
@@ -413,2 +413,43 @@ var pt = require('./index.js'); | ||
}); | ||
describe('indent', function () { | ||
it('should indent a trie with a branch', function () { | ||
expect(pt.ParamTrie.of('a').indent([pt.ParamBranch.Branch('foo')])).to.eq(pt.ParamTrie.ofPath([pt.ParamBranch.Branch('foo')], 'a')); | ||
}); | ||
it('should indent a trie with some branches', function () { | ||
expect(pt.ParamTrie.of('a').indent([ | ||
pt.ParamBranch.Branch('foo'), | ||
pt.ParamBranch.Branch('bar') | ||
])).to.eq(pt.ParamTrie.ofPath([ | ||
pt.ParamBranch.Branch('foo'), | ||
pt.ParamBranch.Branch('bar') | ||
], 'a')); | ||
}); | ||
it('should indent a trie with a param', function () { | ||
expect(pt.ParamTrie.of('a').indent([pt.ParamBranch.Param('foo')])).to.eq(pt.ParamTrie.ofPath([pt.ParamBranch.Param('foo')], 'a')); | ||
}); | ||
it('should indent a trie with some branches', function () { | ||
expect(pt.ParamTrie.of('a').indent([ | ||
pt.ParamBranch.Param('foo'), | ||
pt.ParamBranch.Param('bar') | ||
])).to.eq(pt.ParamTrie.ofPath([ | ||
pt.ParamBranch.Param('foo'), | ||
pt.ParamBranch.Param('bar') | ||
], 'a')); | ||
}); | ||
it('should indent a trie with a mixed path', function () { | ||
expect(pt.ParamTrie.of('a').indent([ | ||
pt.ParamBranch.Param('foo'), | ||
pt.ParamBranch.Branch('bar') | ||
])).to.eq(pt.ParamTrie.ofPath([ | ||
pt.ParamBranch.Param('foo'), | ||
pt.ParamBranch.Branch('bar') | ||
], 'a')); | ||
}); | ||
it('should indent a trie that already has a phat', function () { | ||
expect(pt.ParamTrie.ofPath([pt.ParamBranch.Branch('foo')], 'a').indent([pt.ParamBranch.Branch('bar')])).to.eq(pt.ParamTrie.ofPath([ | ||
pt.ParamBranch.Branch('bar'), | ||
pt.ParamBranch.Branch('foo') | ||
], 'a')); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
225517
1.83%725
6.93%92
5.75%