brisky-stamp
Advanced tools
Comparing version 2.1.6 to 3.0.0
143
lib/index.js
@@ -1,23 +0,43 @@ | ||
if (global.briskystamp) { | ||
module.exports = global.briskystamp | ||
} else { | ||
if (!global.briskystamp) { | ||
const next = typeof setImmediate === void 0 ? setTimeout : setImmediate | ||
const ts = exports.ts = 535673248076 | ||
var tsInProgress = false | ||
var cnt = 0 | ||
var d | ||
var on | ||
global.briskystamp = exports | ||
var on | ||
exports.inProgress = false | ||
exports.cnt = 0 | ||
// add timestamp option -- maybe allways use it | ||
exports.offset = 0 | ||
const ms = () => { | ||
if (!tsInProgress) { | ||
cnt = 0 | ||
d = Date.now() - ts + exports.offset | ||
tsInProgress = true | ||
next(() => { tsInProgress = false }) | ||
} else { | ||
d += ++cnt / 9999 | ||
} | ||
return (exports.cnt = d) | ||
} | ||
exports.create = (type, src, override) => { | ||
var stamp = override || ++exports.cnt | ||
if (override) override = override - ts | ||
if (type) { | ||
stamp = type + '-' + stamp | ||
return [ override || ms(), src || 0, parseInt(type, 36) ] | ||
} else if (src) { | ||
return [ override || ms(), src ] | ||
} else { | ||
return override || ms() | ||
} | ||
if (src) { | ||
stamp = src + '|' + stamp | ||
} | ||
return stamp | ||
} | ||
exports.src = stamp => stamp && stamp[1] | ||
exports.val = stamp => stamp && ((stamp[0] || stamp) + ts) | ||
exports.type = stamp => stamp[2] && stamp[2].toString(36) | ||
exports.on = fn => { | ||
@@ -31,2 +51,4 @@ if (!on) { | ||
exports.clear = () => { on = false } // rename this to stop | ||
exports.close = () => { | ||
@@ -40,91 +62,16 @@ if (on && !exports.inProgress) { | ||
exports.clear = () => { on = false } | ||
exports.parse = stamp => { | ||
const parsed = {} | ||
const src = exports.src(stamp) | ||
const type = exports.type(stamp) | ||
if (src || type) { | ||
if (src) { | ||
parsed.src = src | ||
if (type) { | ||
parsed.type = type | ||
parsed.val = stamp.slice(stamp.indexOf(type) + type.length + 1) | ||
} else { | ||
parsed.val = stamp.slice(stamp.indexOf('|') + 1) | ||
} | ||
} else { | ||
parsed.type = type | ||
parsed.val = stamp.slice(stamp.indexOf(type) + type.length + 1) | ||
} | ||
if (typeof stamp === 'object') { | ||
const parsed = { val: stamp[0] + ts } | ||
const src = stamp[1] | ||
const type = exports.type(stamp) | ||
if (src) parsed.src = src | ||
if (type) parsed.type = type | ||
return parsed | ||
} else { | ||
parsed.val = stamp | ||
return { val: stamp + ts } | ||
} | ||
return parsed | ||
} | ||
} | ||
exports.src = stamp => { | ||
if (typeof stamp === 'string') { | ||
for (let i = 1, len = stamp.length - 1; i < len; i++) { | ||
if (stamp.charAt(i) === '|') { | ||
return stamp.slice(0, i) | ||
} | ||
} | ||
} | ||
} | ||
exports.val = stamp => { | ||
if (typeof stamp === 'string') { | ||
for (let i = stamp.length - 1; i > 0; i--) { | ||
if (stamp.charAt(i) === '-') { | ||
return stamp.slice(i + 1) | ||
} | ||
} | ||
for (let i = 1, len = stamp.length - 1; i < len; i++) { | ||
if (stamp.charAt(i) === '|') { | ||
return stamp.slice(i + 1) | ||
} | ||
} | ||
return stamp | ||
} else { | ||
return stamp | ||
} | ||
} | ||
exports.hasSrc = stamp => { | ||
if (typeof stamp === 'string') { | ||
for (let i = 1, len = stamp.length - 1; i < len; i++) { | ||
if (stamp.charAt(i) === '|') { | ||
return i | ||
} | ||
} | ||
} | ||
} | ||
exports.setSrc = (stamp, val) => val + '|' + stamp | ||
exports.type = (stamp, src) => { | ||
if (typeof stamp === 'string') { | ||
let index | ||
if (!src) { | ||
src = -1 | ||
for (let j = 1; j < stamp.length - 1; j++) { | ||
if (stamp.charAt(j) === '|') { | ||
src = j | ||
break | ||
} | ||
} | ||
} | ||
for (let i = stamp.length - 1; i > src + 1; i--) { | ||
let char = stamp.charAt(i) | ||
if (char === '-') { | ||
index = i | ||
break | ||
} | ||
} | ||
if (index) { | ||
return stamp.slice(src + 1, index) | ||
} | ||
} | ||
} | ||
} | ||
export default global.briskystamp |
{ | ||
"name": "brisky-stamp", | ||
"version": "2.1.6", | ||
"version": "3.0.0", | ||
"description": "Generate unique meta information for change in a system", | ||
"main": "lib/index.js", | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"jsnext:main": "dist/index.es.js", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"watch": "rollup -c -w", | ||
"prepublish": "npm run build", | ||
"pretest": "npm run build", | ||
"test": "node test | tap-difflet && standard", | ||
"dev": "nodemon test | tap-difflet", | ||
"cover": "istanbul cover --report none --print detail test/index.js", | ||
"docs": "node_modules/vigour-doc/bin/vdoc", | ||
"travis": "npm run cover -s && istanbul report lcov && ((cat coverage/lcov.info | coveralls) || exit 0) && standard" | ||
@@ -22,10 +27,2 @@ }, | ||
], | ||
"dependencies": { | ||
"bubleify": "0.5.1" | ||
}, | ||
"browserify": { | ||
"transform": [ | ||
"bubleify" | ||
] | ||
}, | ||
"devDependencies": { | ||
@@ -36,2 +33,4 @@ "coveralls": "^2.11.9", | ||
"pre-commit": "^1.1.3", | ||
"rollup": "^0.37.0", | ||
"rollup-watch": "^2.5.0", | ||
"standard": "^8.4.0", | ||
@@ -38,0 +37,0 @@ "tap-difflet": "0.4.0", |
# brisky-stamp | ||
Generate unique meta information for change in a system | ||
Generate unique meta information for change in a system, monotonic timestamps | ||
@@ -10,26 +10,29 @@ [![Build Status](https://travis-ci.org/vigour-io/brisky-stamp.svg?branch=master)](https://travis-ci.org/vigour-io/brisky-stamp) | ||
```javascript | ||
const briskyStamp = require('brisky-stamp') | ||
const stamp = require('brisky-stamp') | ||
// briskyStamp.create(type, source, override) | ||
// results in click-1 | ||
const stamp = briskyStamp.create('click') | ||
// stamp.create(type, source, override) | ||
// results in [ 946725040140, 0, 21158948 ] | ||
// only lowercase letters from the alphabet are supported (base(36)) | ||
console.log(stamp.create('click')) | ||
// fires when a stamp closes (is handled) | ||
briskyStamp.on(() => console.log('closing')) | ||
stamp.on(() => console.log('closing')) | ||
// fires stamp listeners, usefull for debugging (all stamps need to be closed) | ||
briskyStamp.close(stamp) | ||
// fires stamp listeners | ||
stamp.close() | ||
const parsed = briskyStamp.parse(stamp) | ||
const parsed = stamp.parse(stamp) | ||
// returns a parsed stamp { type: 'click', val: 1 } | ||
// remove all listeners | ||
briskyStamp.clear() | ||
stamp.clear() | ||
// to debug use | ||
const debug = require('brisky-stamp/debug') | ||
debug(briskyStamp) | ||
// this will throw errors when stamps are created while others are still open | ||
// idea is that stamps are synchronous, so you never have 2 stamps in progress at the same time in one thread | ||
briskyStamp.create(type, source, override, true) // ignores stamp creation for debugging | ||
// offset internal clock | ||
stamp.offset = 100 // add 100 ms to timestamps | ||
// check if listeners are in progress | ||
stamp.inProgress // true or false | ||
``` | ||
**note** Types only support base 36 characters - beware! (lowercase letters from the alphabet) |
'use strict' | ||
require('./method') | ||
require('./debug') | ||
// require('./perf') |
@@ -1,18 +0,4 @@ | ||
'use strict' | ||
const test = require('tape') | ||
var bstamp | ||
var bstamp = require('../') | ||
test('create stamps', t => { | ||
bstamp = require('../') | ||
const gbstamp = global.briskystamp | ||
t.plan(4) | ||
stamp('default', gbstamp.cnt + 1, bstamp.create()) | ||
stamp('type', 'click-' + (gbstamp.cnt + 1), bstamp.create('click')) | ||
stamp('source', 'mac|' + (gbstamp.cnt + 1), bstamp.create(void 0, 'mac')) | ||
stamp('all', 'mac|click-override', bstamp.create('click', 'mac', 'override')) | ||
function stamp (label, result, val) { | ||
t.equal(val, result, label + ' stamp generates "' + result + '"') | ||
} | ||
}) | ||
test('parse src', t => { | ||
@@ -22,8 +8,4 @@ var stamp = bstamp.create('click', 'mac') | ||
stamp = bstamp.create('click') | ||
stamp = bstamp.setSrc(stamp, 'iphone') | ||
t.equal(bstamp.src(stamp), 'iphone', 'extracts src after setSrc') | ||
t.equal(bstamp.hasSrc(stamp), 6, 'correct hasSrc') | ||
t.equal(bstamp.src('client|8'), 'client') | ||
t.equal(bstamp.hasSrc('client|8'), 6) | ||
t.same(bstamp.parse('client|8'), { val: '8', src: 'client' }) | ||
t.equal(bstamp.src(bstamp.create(false, 'client')), 'client') | ||
t.same(bstamp.parse(bstamp.create(false, 'client', 8)), { val: 8, src: 'client' }) | ||
t.end() | ||
@@ -40,30 +22,24 @@ }) | ||
t.plan(9) | ||
const stamp = bstamp.create('special-type-of-stamp', 1, 222132123123.001) | ||
t.equal(bstamp.type(stamp), 'special-type-of-stamp', 'correct type') | ||
t.equal(bstamp.parse(stamp).type, 'special-type-of-stamp', 'correct type (from parse)') | ||
t.equal(bstamp.parse(stamp).val, '222132123123.001', 'correct val') | ||
t.equal(bstamp.val(stamp), '222132123123.001', 'correct val') | ||
t.equal(bstamp.val(100), 100, 'correct val when not a string') | ||
t.equal(bstamp.val('hello'), 'hello', 'correct val when a string but no src and no type') | ||
const stamp2 = bstamp.create(void 0, 1, 222132123123.001) | ||
t.equal(bstamp.val(stamp2), '222132123123.001', 'correct val when no type') | ||
const stamp3 = bstamp.create('special-type-of-stamp', void 0, 222132123123.001) | ||
t.equal(bstamp.val(stamp3), '222132123123.001', 'correct .val when no src') | ||
t.equal(bstamp.parse(stamp3).val, '222132123123.001', 'correct parse.val when no src') | ||
const stamp = bstamp.create('special', 1, 222132123123.01) | ||
t.equal(bstamp.type(stamp), 'special', 'correct type') | ||
t.equal(bstamp.parse(stamp).type, 'special', 'correct type (from parse)') | ||
t.equal(bstamp.parse(stamp).val, 222132123123.01, 'correct val') | ||
t.equal(bstamp.val(stamp), 222132123123.01, 'correct val') | ||
t.equal(bstamp.val(100 - bstamp.ts), 100, 'correct val when not a string') | ||
t.equal(bstamp.val(222 - bstamp.ts), 222, 'correct val when a string but no src and no type') | ||
const stamp2 = bstamp.create(void 0, 1, 222132123123.01) | ||
t.equal(bstamp.val(stamp2), 222132123123.01, 'correct val when no type') | ||
const stamp3 = bstamp.create('special-type-of-stamp', void 0, 222132123123.01) | ||
t.equal(bstamp.val(stamp3), 222132123123.01, 'correct .val when no src') | ||
t.equal(bstamp.parse(stamp3).val, 222132123123.01, 'correct parse.val when no src') | ||
}) | ||
test('parse stamps', 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', type: 'type|type-val' } | ||
) | ||
parse('source-source|val', { src: 'source-source', val: 'val' }) | ||
parse('val', { val: 'val' }) | ||
parse('type-val', { type: 'type', val: 'val' }) | ||
parse(bstamp.create('type', 'source', 1), { src: 'source', type: 'type', val: 1 }) | ||
parse(bstamp.create(false, 'source', 1), { src: 'source', val: 1 }) | ||
parse(bstamp.create(false, false, 1), { val: 1 }) | ||
function parse (val, result) { | ||
t.deepEqual(bstamp.parse(val), result, '"' + val + '" parsed correctly') | ||
} | ||
t.end() | ||
}) | ||
@@ -70,0 +46,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
38
99090
9
11
240
1
- Removedbubleify@0.5.1
- Removedacorn@3.3.0(transitive)
- Removedacorn-jsx@3.0.1(transitive)
- Removedacorn-object-spread@1.0.0(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedbuble@0.12.5(transitive)
- Removedbubleify@0.5.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedmagic-string@0.14.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedvlq@0.2.3(transitive)