Skip to content

Commit

Permalink
支持普通的递增id和mongoldb的BSON.objectID
Browse files Browse the repository at this point in the history
  • Loading branch information
filow committed Apr 20, 2016
1 parent 3fa46c5 commit a3646ab
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/random/functions/id.js
@@ -1 +1,11 @@
"use strict";
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.id = id;
let store = 0;
function id() {
store++;
return store;
}
32 changes: 32 additions & 0 deletions lib/random/functions/mongoid.js
@@ -0,0 +1,32 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mongoid = mongoid;
const MACHINE_ID = parseInt(Math.random() * 0xFFFFFF, 10);
const PID = typeof process === 'undefined' || (typeof process.pid !== 'number' ? Math.floor(Math.random() * 100000) : process.pid) % 0xFFFF;
let index = parseInt(Math.random() * 0xFFFFFF, 10);
function toFixedHex(val, length) {
if (typeof val === 'number') {
return this.pad(val.toString(16).substring(0, length), length);
}
return this.pad(val.substring(0, length), length);
}
function mongoid() {
let params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var _params$timestamp = params.timestamp;
const timestamp = _params$timestamp === undefined ? Number(new Date()) : _params$timestamp;
var _params$machine = params.machine;
const machine = _params$machine === undefined ? MACHINE_ID : _params$machine;
var _params$pid = params.pid;
const pid = _params$pid === undefined ? PID : _params$pid;

let result = '';
result += toFixedHex.call(this, Math.floor(timestamp / 1000), 8);
result += toFixedHex.call(this, machine, 6);
result += toFixedHex.call(this, pid, 4);
result += toFixedHex.call(this, index, 6);
index++;
return result;
}
5 changes: 5 additions & 0 deletions src/random/functions/id.js
@@ -0,0 +1,5 @@
let store = 0;
export function id() {
store++;
return store;
}
20 changes: 20 additions & 0 deletions src/random/functions/mongoid.js
@@ -0,0 +1,20 @@
const MACHINE_ID = parseInt(Math.random() * 0xFFFFFF, 10);
const PID = typeof process === 'undefined' ||
(typeof process.pid !== 'number' ? Math.floor(Math.random() * 100000) : process.pid) % 0xFFFF;
let index = parseInt(Math.random() * 0xFFFFFF, 10);
function toFixedHex(val, length) {
if (typeof val === 'number') {
return this.pad(val.toString(16).substring(0, length), length);
}
return this.pad(val.substring(0, length), length);
}
export function mongoid(params = {}) {
const { timestamp = Number(new Date()), machine = MACHINE_ID, pid = PID } = params;
let result = '';
result += toFixedHex.call(this, Math.floor(timestamp / 1000), 8);
result += toFixedHex.call(this, machine, 6);
result += toFixedHex.call(this, pid, 4);
result += toFixedHex.call(this, index, 6);
index++;
return result;
}
38 changes: 38 additions & 0 deletions test/random.js
Expand Up @@ -235,4 +235,42 @@ describe('Random#function', () => {
name = random.cnName({ surname: false, name: '火土火土' })();
assert.equal(name.length, 3);
});

it('id', () => {
const t = random.id();
for (let i = 0; i < 10; i++) {
assert.equal(t(), i + 1);
}
});

it('mongoid', () => {
// 格式检查
let t = random.mongoid()();
assert(t.match(/^[0-9a-f]{24}$/));
// 自定义时间戳
const timestamp = Number(new Date(2010, 1, 1));
const expectedTimeHex = Math.floor(timestamp / 1000).toString(16);
t = random.mongoid({ timestamp })();
assert.equal(t.substring(0, 8), expectedTimeHex);
// 记录当前的序号
const baseOrder = parseInt(t.substring(18, 24), 16);
// 检查自定义序号时符号不足是否会补0
t = random.mongoid({ machine: 'ff0' })();
assert.equal(t.substring(8, 14), '000ff0');
// 记录序号
const order = parseInt(t.substring(18, 24), 16);
// 两个序号应该相差1
assert.equal(baseOrder + 1, order);
// 检查自定义序号超过数量会不会截断
t = random.mongoid({ machine: 'ffaabbcc0' })();
assert.equal(t.substring(8, 14), 'ffaabb');
// 检查传入数字会不会转换为hex
t = random.mongoid({ machine: 255 })();
assert.equal(t.substring(8, 14), '0000ff');
// 检查pid
t = random.mongoid({ pid: 255 })();
assert.equal(t.substring(14, 18), '00ff');
t = random.mongoid({ pid: 'abcd' })();
assert.equal(t.substring(14, 18), 'abcd');
});
});

0 comments on commit a3646ab

Please sign in to comment.