Skip to content

Commit

Permalink
fixed broken stamp cases (type parse is a little bit slower) added fa…
Browse files Browse the repository at this point in the history
…st val parser

closes #8, closes #9
  • Loading branch information
Jim de Beer committed Jul 27, 2016
1 parent 2203979 commit e4d321a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
36 changes: 26 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ exports.src = function (stamp) {
}
}

exports.val = function (stamp) {
if (typeof stamp === 'string') {
for (let i = stamp.length - 1; i > 0; i--) {
if (stamp.charAt(i) === '-') {
return stamp.slice(i + 1)
}
}
} else {
return stamp
}
}

exports.hasSrc = function (stamp) {
if (typeof stamp === 'string') {
for (let i = 1, len = stamp.length - 1; i < len; i++) {
Expand All @@ -93,23 +105,27 @@ exports.setSrc = function (stamp, val) {
return val + '|' + stamp
}

exports.type = function (stamp) {
exports.type = function (stamp, src) {
if (typeof stamp === 'string') {
let index
let src
for (let i = 1, len = stamp.length - 1; i < len; i++) {
if (!src) {
src = -1
for (let j = 1; j < stamp.length - 2; j++) {
if (stamp.charAt(j) === '|') {
src = j
break
}
}
}
for (let i = stamp.length - 2; i > src + 1; i--) {
let char = stamp.charAt(i)
if (char === '-') {
index = i
if (src) {
return stamp.slice(src + 1, index)
}
} else if (!src && char === '|') {
src = i
break
}
}
if (index && !src) {
return stamp.slice(0, index)
if (index) {
return stamp.slice(src + 1, index)
}
}
}
12 changes: 11 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ test('parse type', function (t) {
t.end()
})

test('parse stamps - val', function (t) {
t.plan(5)
const stamp = vstamp.create('special-type-of-stamp', 1, 222132123123.001)
t.equal(vstamp.type(stamp), 'special-type-of-stamp', 'correct type')
t.equal(vstamp.parse(stamp).type, 'special-type-of-stamp', 'correct type (from parse)')
t.equal(vstamp.parse(stamp).val, '222132123123.001', 'correct val')
t.equal(vstamp.val(stamp), '222132123123.001', 'correct val')
t.equal(vstamp.val(100), 100, 'correct val when not a string')
})

test('parse stamps', function (t) {
t.plan(6)
parse('source|type-val', { src: 'source', type: 'type', val: 'val' })
parse('source|val', { src: 'source', val: 'val' })
parse(
'source-source|type|type-val-val|val',
{ src: 'source-source', val: 'val-val|val', type: 'type|type' }
{ src: 'source-source', val: 'val|val', type: 'type|type-val' }
)
parse('source-source|val', { src: 'source-source', val: 'val' })
parse('val', { val: 'val' })
Expand Down
Empty file added test/performance/index.js
Empty file.

0 comments on commit e4d321a

Please sign in to comment.