diff --git a/lib/index.js b/lib/index.js index b6a32c9..fbadb9a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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++) { @@ -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) } } } diff --git a/test/index.js b/test/index.js index ad03d86..8b1c561 100644 --- a/test/index.js +++ b/test/index.js @@ -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' }) diff --git a/test/performance/index.js b/test/performance/index.js new file mode 100644 index 0000000..e69de29