mirror of
https://github.com/Myxelium/Bridge-Multi.git
synced 2026-07-09 11:35:08 +00:00
Various refactoring
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
let sanitize = require('sanitize-filename')
|
||||
const sanitize = require('sanitize-filename')
|
||||
|
||||
/**
|
||||
* @returns A random UUID
|
||||
* @returns a random UUID
|
||||
*/
|
||||
export function generateUUID() { // Public Domain/MIT
|
||||
var d = new Date().getTime()//Timestamp
|
||||
var d2 = Date.now() // Time in microseconds since page-load or 0 if unsupported
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
var r = Math.random() * 16//random number between 0 and 16
|
||||
if (d > 0) {//Use timestamp until depleted
|
||||
let d = new Date().getTime() // Timestamp
|
||||
let d2 = Date.now() // Time in microseconds since page-load or 0 if unsupported
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
let r = Math.random() * 16 // Random number between 0 and 16
|
||||
if (d > 0) { // Use timestamp until depleted
|
||||
r = (d + r) % 16 | 0
|
||||
d = Math.floor(d / 16)
|
||||
} else {//Use microseconds since page-load if supported
|
||||
} else { // Use microseconds since page-load if supported
|
||||
r = (d2 + r) % 16 | 0
|
||||
d2 = Math.floor(d2 / 16)
|
||||
}
|
||||
@@ -20,9 +20,8 @@ export function generateUUID() { // Public Domain/MIT
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes a filename of any characters that cannot be part of a windows filename.
|
||||
* @param filename The name of the file to sanitize.
|
||||
*/
|
||||
* @returns `filename`, but with any invalid filename characters replaced with similar valid characters.
|
||||
*/
|
||||
export function sanitizeFilename(filename: string): string {
|
||||
const newName = sanitize(filename, {
|
||||
replacement: ((invalidChar: string) => {
|
||||
@@ -30,7 +29,7 @@ export function sanitizeFilename(filename: string): string {
|
||||
case '/': return '-'
|
||||
case '\\': return '-'
|
||||
case '"': return "'"
|
||||
default: return '_' //TODO: add more cases for replacing invalid characters
|
||||
default: return '_' // TODO: add more cases for replacing invalid characters
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -38,16 +37,14 @@ export function sanitizeFilename(filename: string): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts <val> from the range (<fromA>, <fromB>) to the range (<toA>, <toB>).
|
||||
* Converts `val` from the range (`fromA`, `fromB`) to the range (`toA`, `toB`).
|
||||
*/
|
||||
export function interpolate(val: number, fromA: number, fromB: number, toA: number, toB: number) {
|
||||
return ((val - fromA) / (fromB - fromA)) * (toB - toA) + toA
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits <objectList> into multiple arrays, grouping by matching <key> values.
|
||||
* @param objectList A list of objects.
|
||||
* @param key A key from the list of objects.
|
||||
* @returns `objectList` split into multiple arrays, where each array contains the objects with matching `key` values.
|
||||
*/
|
||||
export function groupBy<T>(objectList: T[], key: keyof T) {
|
||||
const results: T[][] = []
|
||||
|
||||
Reference in New Issue
Block a user