-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
363 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
# OpenZhy Utilities # | ||
|
||
Utilities for typescript frontend development | ||
Utilities for typescript frontend development | ||
|
||
## Installation ## | ||
`npm i -S @open-zhy/utils` | ||
OR | ||
`yarn add @open-zhy/utils` |
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 |
---|---|---|
@@ -1,4 +1,25 @@ | ||
/** | ||
* Find intersections of 2 array | ||
* | ||
* @param arr1 | ||
* @param arr2 | ||
* @returns Array of all values that share both arrays | ||
*/ | ||
export const arrayIntersect = <T extends unknown>( | ||
arr1: T[], | ||
arr2: T[] | ||
) => arr1.filter((a1: T) => arr2.includes(a1)); | ||
|
||
/** | ||
* Remove an item from array by its index | ||
* | ||
* @param source | ||
* @param index | ||
* @returns | ||
*/ | ||
export const removeByIndex = <T>(source: Array<T>, index: number): Array<T> => { | ||
const temp: Array<T> = [...source]; | ||
temp.splice(index, 1); | ||
|
||
return temp; | ||
}; |
File renamed without changes.
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,21 @@ | ||
/** | ||
* Convert a bytes number to human readable value | ||
* | ||
* @param bytes | ||
* @param decimals | ||
* @returns | ||
*/ | ||
const bytesToSize = (bytes: number, decimals = 2): string => { | ||
if (bytes === 0) { | ||
return '0 Bytes'; | ||
} | ||
|
||
const k = 1024; | ||
const dm = decimals < 0 ? 0 : decimals; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; | ||
}; | ||
|
||
export default bytesToSize; |
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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
// eslint-disable-next-line max-len | ||
/** | ||
* Test whether the browser is on mobile | ||
* | ||
* @returns | ||
*/ | ||
export const isMobile = (): boolean => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile/i.test(navigator.userAgent); |
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
/** | ||
* Check whether a string is a valid email | ||
* | ||
* @param email ` | ||
* @returns | ||
*/ | ||
export const isValidEmail = (email: string) => (/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/ig).test(email); |
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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
export * from './async'; | ||
export * from './array'; | ||
export * from './bytesToSize'; | ||
export * from './formatNumber'; | ||
export * from './formatTime'; | ||
export * from './images'; | ||
export * from './email'; | ||
export * from './storage'; | ||
export * from './device'; | ||
export * from './waitable'; |
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
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,62 @@ | ||
/** | ||
* Create a random string that can be used as Id | ||
* | ||
* @param factor | ||
* @returns | ||
*/ | ||
export const generateId = (factor: number = 16): string => { | ||
const arr = new Uint8Array(factor); | ||
window.crypto.getRandomValues(arr); | ||
return Array.from(arr, (v) => v.toString(16).padStart(2, '0')).join(''); | ||
}; | ||
|
||
/** | ||
* Capitablize a string | ||
* | ||
* @param text | ||
* @returns | ||
*/ | ||
export const capitalize = (text: string): string => { | ||
if ((text || '').length === 0) { | ||
return ''; | ||
} | ||
|
||
return text[0].toUpperCase() + text.substring(1); | ||
}; | ||
|
||
/** | ||
* Truncate a string | ||
* | ||
* @param str | ||
* @param num | ||
* @returns | ||
*/ | ||
export const truncate = (str: string, num: number = 100): string => { | ||
// If the length of str is less than or equal to num | ||
// just return str--don't truncate it. | ||
if (str.length <= num) { | ||
return str; | ||
} | ||
|
||
// Return str truncated with '...' concatenated to the end of str. | ||
return `${str.slice(0, num)}...`; | ||
}; | ||
|
||
/** | ||
* Simplified text parser | ||
* | ||
* @param originalText | ||
* @param config | ||
* @returns | ||
*/ | ||
export const textParser = (originalText: string, config: any[] = []): { [key: string]: string } => { | ||
const result: { [key: string]: string } = {}; | ||
|
||
let rest: string = originalText; | ||
config.forEach(({ key, search }: { key: string; search: string }) => { | ||
result[key] = rest.split(search).pop(); | ||
rest = rest.replace(result[key], '').replace(search, ''); | ||
}); | ||
|
||
return result; | ||
}; |
Oops, something went wrong.