Update id generation methods
This commit is contained in:
		
							parent
							
								
									b247be80cc
								
							
						
					
					
						commit
						e64912545a
					
				
					 6 changed files with 30 additions and 72 deletions
				
			
		| 
						 | 
				
			
			@ -1,26 +0,0 @@
 | 
			
		|||
// AID
 | 
			
		||||
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さnの[ランダムな文字列]
 | 
			
		||||
 | 
			
		||||
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
 | 
			
		||||
const TIME2000 = 946684800000;
 | 
			
		||||
 | 
			
		||||
function getTime(time: number) {
 | 
			
		||||
	time = time - TIME2000;
 | 
			
		||||
	if (time < 0) time = 0;
 | 
			
		||||
 | 
			
		||||
	return time.toString(36);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getRandom(length: number) {
 | 
			
		||||
	let str = '';
 | 
			
		||||
 | 
			
		||||
	for (let i = 0; i < length; i++) {
 | 
			
		||||
		str += CHARS[Math.floor(Math.random() * CHARS.length)];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return str;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function genAid(date: Date, rand: number): string {
 | 
			
		||||
	return getTime(date.getTime()).padStart(8, CHARS[0]) + getRandom(rand);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,26 +0,0 @@
 | 
			
		|||
// AID(Cheep)
 | 
			
		||||
// 長さ6の[2000年1月1日からの経過秒をbase36でエンコードしたもの] + 長さ3の[ランダムな文字列]
 | 
			
		||||
 | 
			
		||||
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
 | 
			
		||||
const TIME2000 = 946684800000;
 | 
			
		||||
 | 
			
		||||
function getTime(time: number) {
 | 
			
		||||
	time = time - TIME2000;
 | 
			
		||||
	if (time < 0) time = 0;
 | 
			
		||||
	time = Math.floor(time / 1000);
 | 
			
		||||
	return time.toString(36);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getRandom() {
 | 
			
		||||
	let str = '';
 | 
			
		||||
 | 
			
		||||
	for (let i = 0; i < 3; i++) {
 | 
			
		||||
		str += CHARS[Math.floor(Math.random() * CHARS.length)];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return str;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function genAidc(date: Date): string {
 | 
			
		||||
	return getTime(date.getTime()).padStart(6, CHARS[0]) + getRandom();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,7 +1,6 @@
 | 
			
		|||
import { ulid } from 'ulid';
 | 
			
		||||
import { genAid } from './aid';
 | 
			
		||||
import { genAidc } from './aidc';
 | 
			
		||||
import { genObjectId } from './object-id';
 | 
			
		||||
import { genAid } from './id/aid';
 | 
			
		||||
import { genObjectId } from './id/object-id';
 | 
			
		||||
import config from '../config';
 | 
			
		||||
 | 
			
		||||
const metohd = config.id.toLowerCase();
 | 
			
		||||
| 
						 | 
				
			
			@ -10,11 +9,7 @@ export function genId(date?: Date): string {
 | 
			
		|||
	if (!date || (date > new Date())) date = new Date();
 | 
			
		||||
 | 
			
		||||
	switch (metohd) {
 | 
			
		||||
		case 'aidc': return genAidc(date);
 | 
			
		||||
		case 'aid1': return genAid(date, 1);
 | 
			
		||||
		case 'aid2': return genAid(date, 2);
 | 
			
		||||
		case 'aid3': return genAid(date, 3);
 | 
			
		||||
		case 'aid4': return genAid(date, 4);
 | 
			
		||||
		case 'aid': return genAid(date);
 | 
			
		||||
		case 'ulid': return ulid(date.getTime());
 | 
			
		||||
		case 'objectid': return genObjectId(date);
 | 
			
		||||
		default: throw 'unknown id generation method';
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										23
									
								
								src/misc/id/aid.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/misc/id/aid.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
// AID
 | 
			
		||||
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
 | 
			
		||||
 | 
			
		||||
import * as cluster from 'cluster';
 | 
			
		||||
 | 
			
		||||
const TIME2000 = 946684800000;
 | 
			
		||||
let counter = process.pid + (cluster.isMaster ? 0 : cluster.worker.id);
 | 
			
		||||
 | 
			
		||||
function getTime(time: number) {
 | 
			
		||||
	time = time - TIME2000;
 | 
			
		||||
	if (time < 0) time = 0;
 | 
			
		||||
 | 
			
		||||
	return time.toString(36).padStart(8, '0');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getRandom() {
 | 
			
		||||
	return counter.toString(36).padStart(length, '0').substr(2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function genAid(date: Date): string {
 | 
			
		||||
	counter++;
 | 
			
		||||
	return getTime(date.getTime()) + getRandom();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -8,7 +8,7 @@ function getTime(time: number) {
 | 
			
		|||
 | 
			
		||||
	time = Math.floor(time / 1000);
 | 
			
		||||
 | 
			
		||||
	return time.toString(16);
 | 
			
		||||
	return time.toString(16).padStart(8, CHARS[0]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getRandom() {
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue