-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: store temporary files in unique folder to avoid race conditions
- Loading branch information
Showing
4 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2022, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import { randomAlphanumeric } from '../random'; | ||
|
||
describe('randomAlphanumeric', () => { | ||
test('returns random string whose length matches requested length', () => { | ||
for (let len = 0; len < 100; len ++) { | ||
const str = randomAlphanumeric(len); | ||
expect(str).toHaveLength(len); | ||
} | ||
}); | ||
|
||
test('returns empty string when length requested is less than 1', () => { | ||
const str = randomAlphanumeric(0); | ||
expect(str).toBe(''); | ||
|
||
}); | ||
|
||
test('returns empty string when length parameter is negative number', () => { | ||
const str = randomAlphanumeric(-15); | ||
expect(str).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2022, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
const ALPHANUMERIC_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
const ALPHANUMERIC_CHARACTERS_LENGTH = ALPHANUMERIC_CHARACTERS.length; | ||
|
||
/** | ||
* Creates a random alphanumeric string whose length is the number of characters specified. | ||
* @param length The length of the random string to create | ||
* @returns The random string | ||
*/ | ||
export const randomAlphanumeric = (length: Number): string => { | ||
if (length < 1) { | ||
return ""; | ||
} | ||
|
||
let randomString = ""; | ||
|
||
for (let i = 0; i < length; i++) { | ||
// Note: Math.random returns a decimal value between 0 (inclusive) and 1 (exclusive). | ||
// Since it will never return a 1 and we are doing Math.floor here, the index will never | ||
// be larger than (ALPHANUMERIC_CHARACTERS_LENGTH-1) | ||
const index = Math.floor(Math.random() * ALPHANUMERIC_CHARACTERS_LENGTH); | ||
randomString += ALPHANUMERIC_CHARACTERS[index]; | ||
} | ||
|
||
return randomString; | ||
} |