Skip to content

Commit efe2000

Browse files
authored
Extract uuid function into @firebase/util (#6363)
Removed duplicate uuid function and extracted into `@firebase/util`
1 parent 421fc3b commit efe2000

File tree

8 files changed

+44
-25
lines changed

8 files changed

+44
-25
lines changed

.changeset/tame-rice-peel.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@firebase/app-check": patch
3+
"@firebase/database": patch
4+
"@firebase/util": patch
5+
---
6+
7+
Extract uuid function into @firebase/util

common/api-review/util.api.md

+3
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ export interface Subscribe<T> {
422422
// @public (undocumented)
423423
export type Unsubscribe = () => void;
424424

425+
// @public
426+
export const uuidv4: () => string;
427+
425428
// Warning: (ae-missing-release-tag) "validateArgCount" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
426429
//
427430
// @public

packages/app-check/src/storage.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { uuidv4 } from './util';
1918
import { FirebaseApp } from '@firebase/app';
20-
import { isIndexedDBAvailable } from '@firebase/util';
19+
import { isIndexedDBAvailable, uuidv4 } from '@firebase/util';
2120
import {
2221
readDebugTokenFromIndexedDB,
2322
readTokenFromIndexedDB,

packages/app-check/src/util.ts

-11
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ export function ensureActivated(app: FirebaseApp): void {
3737
}
3838
}
3939

40-
/**
41-
* Copied from https://stackoverflow.com/a/2117523
42-
*/
43-
export function uuidv4(): string {
44-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
45-
const r = (Math.random() * 16) | 0,
46-
v = c === 'x' ? r : (r & 0x3) | 0x8;
47-
return v.toString(16);
48-
});
49-
}
50-
5140
export function getDurationString(durationInMillis: number): string {
5241
const totalSeconds = Math.round(durationInMillis / 1000);
5342
const days = Math.floor(totalSeconds / (3600 * 24));

packages/database/test/helpers/util.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { uuidv4 } from '@firebase/util';
19+
1820
import { Database, ref } from '../../src';
1921
import { ConnectionTarget } from '../../src/api/test_access';
2022

@@ -78,18 +80,6 @@ export function waitFor(waitTimeInMS: number) {
7880
return new Promise(resolve => setTimeout(resolve, waitTimeInMS));
7981
}
8082

81-
/**
82-
* Copied from https://stackoverflow.com/a/2117523
83-
* TODO: extract this into @firebase/util
84-
*/
85-
export function uuidv4(): string {
86-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
87-
const r = (Math.random() * 16) | 0,
88-
v = c === 'x' ? r : (r & 0x3) | 0x8;
89-
return v.toString(16);
90-
});
91-
}
92-
9383
// Creates a unique reference using uuid
9484
export function getUniqueRef(db: Database) {
9585
const path = uuidv4();

packages/util/index.node.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export * from './src/sha1';
3636
export * from './src/subscribe';
3737
export * from './src/validation';
3838
export * from './src/utf8';
39+
export * from './src/uuid';
3940
export * from './src/exponential_backoff';
4041
export * from './src/formatters';
4142
export * from './src/compat';

packages/util/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export * from './src/sha1';
3131
export * from './src/subscribe';
3232
export * from './src/validation';
3333
export * from './src/utf8';
34+
export * from './src/uuid';
3435
export * from './src/exponential_backoff';
3536
export * from './src/formatters';
3637
export * from './src/compat';

packages/util/src/uuid.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Copied from https://stackoverflow.com/a/2117523
20+
* Generates a new uuid.
21+
* @public
22+
*/
23+
export const uuidv4 = function (): string {
24+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
25+
const r = (Math.random() * 16) | 0,
26+
v = c === 'x' ? r : (r & 0x3) | 0x8;
27+
return v.toString(16);
28+
});
29+
};

0 commit comments

Comments
 (0)