Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
feat: add rang and isNullOrEmpty utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Oct 8, 2019
1 parent e15c744 commit beb5f73
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@
* @returns array of given value
*/
export const forceArray = (x: any) => (Array.isArray(x) ? x : [x]);

/**
* create array of length n with from offset
* @param {number} len
* @param {number} [offset=0]
* @param {number} [step=1]
*/
export const range = (len: number, offset: number = 0, step: number = 1) =>
[...Array(len).keys()].map(i => i * step + offset);
4 changes: 2 additions & 2 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const isNumber = (n: any) => typeof n === 'number' && !Number.isNaN(n);
* @returns result | fallback
*/
export const safeDivide = (dividend: number, divisor: number, fallback: any = 0) =>
divisor === 0 ? 0 : dividend / divisor;
divisor === 0 ? fallback : dividend / divisor;

/**
* clamp a number between two values
* @param {number} value
* @param {number[]} [[min, max]=[0, 1]]
* @returns clamped number
*/
export const clamp = (value: number, [min, max] = [0, 1]) => Math.max(Math.min(value, max), min);
export const clamp = (value: number, [min, max]: number[] = [0, 1]) => Math.max(Math.min(value, max), min);

/**
* returns percentage value of a number from a maximum number
Expand Down
9 changes: 8 additions & 1 deletion src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const kebabToCamel = (str: string) =>
* @example
* getInitials('frontend monsters'); // FM
*/
export const getInitials = (name: string, fallback = '?') =>
export const getInitials = (name: string, fallback: string = '?') =>
typeof name === 'string' && name.length > 0
? name
.replace(/\s+/g, ' ')
Expand All @@ -40,3 +40,10 @@ export const getInitials = (name: string, fallback = '?') =>
.map(v => v && v[0].toUpperCase())
.join('')
: fallback;

/**
* check if string is null or empty string
* @param {string} str
* @returns
*/
export const isNullOrEmpty = (str: string) => str == null || str === '';
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dom",
"es2018"
],
"downlevelIteration": true,
"sourceMap": true,
"declaration": true,
"declarationDir": "lib/types",
Expand Down

0 comments on commit beb5f73

Please sign in to comment.