Skip to content

Commit

Permalink
Extract uuid function into @firebase/util (#6363)
Browse files Browse the repository at this point in the history
Removed duplicate uuid function and extracted into `@firebase/util`
  • Loading branch information
maneesht committed Jun 16, 2022
1 parent 421fc3b commit efe2000
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .changeset/tame-rice-peel.md
@@ -0,0 +1,7 @@
---
"@firebase/app-check": patch
"@firebase/database": patch
"@firebase/util": patch
---

Extract uuid function into @firebase/util
3 changes: 3 additions & 0 deletions common/api-review/util.api.md
Expand Up @@ -422,6 +422,9 @@ export interface Subscribe<T> {
// @public (undocumented)
export type Unsubscribe = () => void;

// @public
export const uuidv4: () => string;

// Warning: (ae-missing-release-tag) "validateArgCount" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public
Expand Down
3 changes: 1 addition & 2 deletions packages/app-check/src/storage.ts
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*/

import { uuidv4 } from './util';
import { FirebaseApp } from '@firebase/app';
import { isIndexedDBAvailable } from '@firebase/util';
import { isIndexedDBAvailable, uuidv4 } from '@firebase/util';
import {
readDebugTokenFromIndexedDB,
readTokenFromIndexedDB,
Expand Down
11 changes: 0 additions & 11 deletions packages/app-check/src/util.ts
Expand Up @@ -37,17 +37,6 @@ export function ensureActivated(app: FirebaseApp): void {
}
}

/**
* Copied from https://stackoverflow.com/a/2117523
*/
export function uuidv4(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

export function getDurationString(durationInMillis: number): string {
const totalSeconds = Math.round(durationInMillis / 1000);
const days = Math.floor(totalSeconds / (3600 * 24));
Expand Down
14 changes: 2 additions & 12 deletions packages/database/test/helpers/util.ts
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import { uuidv4 } from '@firebase/util';

import { Database, ref } from '../../src';
import { ConnectionTarget } from '../../src/api/test_access';

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

/**
* Copied from https://stackoverflow.com/a/2117523
* TODO: extract this into @firebase/util
*/
export function uuidv4(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

// Creates a unique reference using uuid
export function getUniqueRef(db: Database) {
const path = uuidv4();
Expand Down
1 change: 1 addition & 0 deletions packages/util/index.node.ts
Expand Up @@ -36,6 +36,7 @@ export * from './src/sha1';
export * from './src/subscribe';
export * from './src/validation';
export * from './src/utf8';
export * from './src/uuid';
export * from './src/exponential_backoff';
export * from './src/formatters';
export * from './src/compat';
1 change: 1 addition & 0 deletions packages/util/index.ts
Expand Up @@ -31,6 +31,7 @@ export * from './src/sha1';
export * from './src/subscribe';
export * from './src/validation';
export * from './src/utf8';
export * from './src/uuid';
export * from './src/exponential_backoff';
export * from './src/formatters';
export * from './src/compat';
29 changes: 29 additions & 0 deletions packages/util/src/uuid.ts
@@ -0,0 +1,29 @@
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Copied from https://stackoverflow.com/a/2117523
* Generates a new uuid.
* @public
*/
export const uuidv4 = function (): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};

0 comments on commit efe2000

Please sign in to comment.