Skip to content

Commit 2d90557

Browse files
committed
feat(uniqueid): adding uniqueid utility
1 parent 416f0b8 commit 2d90557

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

packages/utilities/src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export * from './sameHeight';
1616
export * from './serialize';
1717
export * from './settings';
1818
export * from './smoothScroll';
19+
export * from './uniqueid';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { uniqueid } from '../';
2+
3+
describe('Unique ID utility', () => {
4+
it('should generate a unique ID', () => {
5+
const id1 = uniqueid();
6+
const id2 = uniqueid();
7+
expect(id1).toBe('id1');
8+
expect(id2).toBe('id2');
9+
});
10+
11+
it('should generate a unique ID with user defined prefix', () => {
12+
const id1 = uniqueid('prefix');
13+
const id2 = uniqueid('prefix-');
14+
expect(id1).toBe('prefix3');
15+
expect(id2).toBe('prefix-4');
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as uniqueid } from './uniqueid';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Stores the latast id to increment
3+
* @type {number} Number stored for the next ID
4+
* @private
5+
*/
6+
let _lastId = 0;
7+
8+
/**
9+
* Creates a unique ID to use
10+
*
11+
* @param {string=} prefix Prefix to set for the id
12+
* @returns {string} Unique ID
13+
*/
14+
function uniqueid(prefix = 'id') {
15+
_lastId++;
16+
return `${prefix}${_lastId}`;
17+
}
18+
19+
export default uniqueid;

0 commit comments

Comments
 (0)