Commit 2d90557 1 parent 416f0b8 commit 2d90557 Copy full SHA for 2d90557
File tree 5 files changed +45
-0
lines changed
packages/utilities/src/utilities
5 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -16,3 +16,4 @@ export * from './sameHeight';
16
16
export * from './serialize' ;
17
17
export * from './settings' ;
18
18
export * from './smoothScroll' ;
19
+ export * from './uniqueid' ;
File renamed without changes.
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments