2017-02-20 18:32:27 +00:00
|
|
|
import uuid from 'uuid';
|
2017-03-08 05:52:17 +00:00
|
|
|
import BaseExtension from './base/base-extension';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
|
|
|
export default class UuidExtension extends BaseExtension {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.tags = ['uuid'];
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
run (context, uuidType = 'v4') {
|
|
|
|
if (typeof uuidType === 'number') {
|
|
|
|
uuidType += '';
|
|
|
|
} else if (typeof uuidType === 'string') {
|
|
|
|
uuidType = uuidType.toLowerCase();
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
switch (uuidType) {
|
2017-02-20 18:32:27 +00:00
|
|
|
case '1':
|
|
|
|
case 'v1':
|
|
|
|
return uuid.v1();
|
|
|
|
case '4':
|
|
|
|
case 'v4':
|
|
|
|
return uuid.v4();
|
2017-02-27 21:00:13 +00:00
|
|
|
default:
|
|
|
|
throw new Error(`Invalid UUID type "${uuidType}"`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|