Adds a convenient prototype function package.
Getting started
Import the package to the first file you call.
import "prototype-helper";
console.log("10000".toComma());
Prototype Helper
Number
toNumber(): number;
expect((3.141592).toNumber()).toBe(3.141592);
expect((12345).toNumber()).toBe(12345);
expect("3.14".toNumber()).toBe(3.14);
toDecimal(): Decimal;
expect((3.141592).toDecimal()).toStrictEqual(new Decimal(3.141592));
expect((12345).toDecimal()).toStrictEqual(new Decimal(12345));
expect(NaN.toDecimal().isNaN()).toBeTruthy();
toComma(): string;
expect((3141592).toComma()).toBe("3,141,592");
expect((0).toComma()).toBe("0");
expect((-100000).toComma()).toBe("-100,000");
mod(amp: number): number;
expect((7).mod(3)).toBe(1);
expect((100).mod(7)).toBe(2);
div(value: number): number;
expect((10).div(2)).toBe(5);
expect((8).div(3)).toBeCloseTo(2.6667, 4);
mul(value: number): number;
expect((4).mul(3)).toBe(12);
expect((5).mul(0.5)).toBeCloseTo(2.5, 4);
add(value: number): number;
expect((8).add(3)).toEqual(11);
expect((100).add(1.25)).toEqual(101.25);
sub(value: number): number;
expect((8).sub(3)).toEqual(5);
expect((100).sub(1.25)).toEqual(98.75);
fixNumber(length: number): string;
expect((1234).fixNumber(8)).toBe("00001234");
expect((1234).fixNumber(4)).toBe("1234");
fixPoint(length: number): string;
expect((1234).fixPoint(8)).toBe("1234.00000000");
expect((1234.5678).fixPoint(2)).toBe("1234.56");
abs(): number;
expect((-3).abs()).toBe(3);
expect((3.14).abs()).toBe(3.14);
expect((0).abs()).toBe(0);
isFinite(): boolean;
expect((-5).isFinite()).toBe(true);
expect((1 / 0).isFinite()).toBe(false);
expect((-1 / 0).isFinite()).toBe(false);
isNaN(): boolean;
expect(NaN.isNaN()).toBe(true);
expect((0 / 0).isNaN()).toBe(true);
expect((-1).isNaN()).toBe(false);
isInteger(): boolean;
expect((3).isInteger()).toBe(true);
expect((3.0).isInteger()).toBe(true);
expect((-3.1).isInteger()).toBe(false);
addSymbol(space?: string): string;
expect((10).addSymbol()).toBe("+10");
expect((0).addSymbol()).toBe("0");
expect((-10).addSymbol()).toBe("-10");
fromPer(per: number): number;
expect((100).fromPer(50)).toBe(50);
expect((200).fromPer(25)).toBe(50);
expect((50).fromPer(10)).toBe(5);
toPer(per: number): number;
expect((50).toPer(100)).toBe(50);
expect((50).toPer(200)).toBe(25);
expect((5).toPer(50)).toBe(10);
pow(value: number): number;
expect((10).pow(2)).toEqual(100);
expect((2).pow(10)).toEqual(1024);
normalize(): number;
expect((2).normalize()).toEqual(4);
expect((3.1415).normalize()).toEqual(9.86902225);
ceil(point?: number): number;
expect((3.14159).ceil()).toBe(4);
expect((3.14159).ceil(1)).toBe(3.2);
expect((1234).ceil(-2)).toBe(1300);
floor(point?: number): number;
expect((3.14159).floor()).toBe(3);
expect((3.14159).floor(1)).toBe(3.1);
expect((1234).floor(-2)).toBe(1200);
round(point?: number): number;
expect((3.14159).round()).toBe(3);
expect((3.14159).round(1)).toBe(3.1);
expect((3.14159).round(3)).toBe(3.142);
expect((1234).round(-2)).toBe(1200);
String
fixPoint(length: number): string;
const str = "123";
const result = str.fixPoint(2);
expect(result).toEqual("123.00");
fixNumber(length: number): string;
const input = "123.45";
const output = input.fixNumber(5);
expect(output).toBe("00123.45");
leadingChars(chars: string | number, length: number): string;
const str = "123";
const result = str.leadingChars("0", 5);
expect(result).toEqual("00123");
toComma(): string;
expect("123".toComma()).toBe("123");
expect("1234".toComma()).toBe("1,234");
expect("12345".toComma()).toBe("12,345");
toNumber(): number;
expect("3.14".toNumber()).toBe(3.14);
expect("12345".toNumber()).toBe(12345);
addSymbol(space?: string): string;
-
Returns a string with a symbol added in front, where the symbol is "+" if the number is positive, "-" if it is negative, or an empty string if it is zero.
-
숫자에 부호를 추가하여 반환하는 메서드입니다.
expect("123".toComma()).toBe("123");
expect("12345".toComma()).toBe("12,345");
expect("0.12345".toComma()).toBe("0.12345");
expect("".toComma()).toBe("0");
fromJSON(): T;
const jsonString = '{"name": "John", "age": 30}';
const parsedObject = jsonString.fromJson();
expect(parsedObject).toEqual({ name: "John", age: 30 });
issetWord(word: string): boolean;
-
Returns true if the given word exists in the string, false otherwise.
-
주어진 문자열이 해당 문자열에 포함되어 있는지 여부를 반환하는 메서드입니다.
const string = "The quick brown fox jumps over the lazy dog";
expect(string.issetWord("cat")).toBe(false);
getChar(index: number): string;
const string = "Hello, world!";
expect(string.getChar(0)).toBe("H");
expect(string.getChar(7)).toBe("w");
isNumber(): boolean;
expect("42".isNumber()).toBe(true);
expect("0xFF".isNumber()).toBe(true);
expect("()".isNumber()).toBe(false);
expect("hello".isNumber()).toBe(false);
Object
Array
single(predicate: (element: T, index: number) => boolean): T;
-
Returns the only element of a sequence that satisfies a specified condition, and throws an error if more than one such element exists.
-
조건을 만족하는 시퀀스의 유일한 요소를 반환하며, 해당 조건을 만족하는 요소가 두 개 이상 존재하는 경우 에러를 발생시킵니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.single((x) => x === 3);
expect(result).toBe(3);
singleOrDefault(predicate: (element: T, index: number) => boolean, defaultValue?: any): T;
-
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists, and throws an error if more than one such element exists.
-
조건을 만족하는 시퀀스의 유일한 요소를 반환하거나, 해당 조건을 만족하는 요소가 없는 경우 기본값을 반환하며, 해당 조건을 만족하는 요소가 두 개 이상 존재하는 경우 에러를 발생시킵니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.singleOrDefault((x) => x === 3);
expect(result).toBe(3);
where(predicate: (element: T, index: number) => boolean): T[];
const arr = [1, 2, 3, 4, 5];
const result = arr.where((x) => x % 2 === 0);
expect(result).toEqual([2, 4]);
skip(count: number): T[];
const arr = [1, 2, 3, 4, 5];
const result = arr.skip(2);
expect(result).toEqual([3, 4, 5]);
take(count: number): T[];
const arr = [1, 2, 3, 4, 5];
const result = arr.take(3);
expect(result).toEqual([1, 2, 3]);
select(predicate: (element: T, index: number) => R): R[];
const arr = [1, 2, 3, 4, 5];
const result = arr.select((x) => x * 2);
expect(result).toEqual([2, 4, 6, 8, 10]);
any(predicate?: (element: T, index: number) => boolean): boolean;
const arr = [1, 2, 3, 4, 5];
const result = arr.any((x) => x === 3);
expect(result).toBe(true);
count(predicate?: (element: T, index: number) => boolean): number;
const arr = [1, 2, 3, 4, 5];
const result = arr.count();
expect(result).toBe(5);
const arr = [1, 2, 3, 4, 5];
const result = arr.count((x) => x % 2 === 0);
expect(result).toBe(2);
max(predicate?: (element: T, index: number) => boolean): T;
-
Returns the element in the array with the maximum value. The optional predicate parameter can be used to specify a custom condition for the maximum value to be selected.
-
배열에서 가장 큰 값을 가지는 요소를 반환합니다. 선택적 매개변수인 predicate를 사용하여 최대값이 선택되는 조건을 지정할 수 있습니다.
const arr = [1, 3, 5, 4, 2];
const result = arr.max();
expect(result).toBe(5);
min(predicate?: (element: T, index: number) => boolean): T;
-
Returns the element in the array with the minimum value. The optional predicate parameter can be used to specify a custom condition for the minimum value to be selected.
-
배열에서 가장 작은 값을 가지는 요소를 반환합니다. 선택적 매개변수인 predicate를 사용하여 최소값이 선택되는 조건을 지정할 수 있습니다.
const arr = [1, 3, 5, 4, 2];
const result = arr.min();
expect(result).toBe(1);
sum(predicate?: (element: T, index: number) => boolean): number;
- Returns the sum of all the elements in the array. The optional predicate parameter can be used to specify a custom condition for which elements should be included in the sum.
배열의 모든 요소의 합을 반환합니다. 선택적 매개변수인 predicate를 사용하여 합산할 요소를 지정할 수 있습니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.sum();
expect(result).toBe(15);
first(predicate?: (element: T, index: number) => boolean): T;
-
Returns the first element in the array that matches the specified predicate function. If no element matches the predicate, an error is thrown.
-
지정된 predicate 함수와 일치하는 첫 번째 요소를 반환합니다. 일치하는 요소가 없으면 오류가 발생합니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.sum((x) => x % 2 === 0);
expect(result).toBe(6);
firstOrDefault(predicate?: (element: T, index: number) => boolean, defaultValue?: any): T;
-
Returns the first element in the array that matches the specified predicate function, or a default value if no element matches the predicate.
-
지정된 predicate 함수와 일치하는 첫 번째 요소를 반환합니다. 일치하는 요소가 없으면 기본값을 반환합니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.firstOrDefault();
expect(result).toBe(1);
last(predicate?: (element: T, index: number) => boolean): T;
-
Returns the last element in the array that matches the specified predicate function. If no element matches the predicate, an error is thrown.
-
지정된 predicate 함수와 일치하는 마지막 요소를 반환합니다. 일치하는 요소가 없으면 오류가 발생합니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.last();
expect(result).toBe(5);
lastOrDefault(predicate?: (element: T, index: number) => boolean, defaultValue?: any): T;
-
Returns the last element in the array that matches the specified predicate function, or a default value if no element matches the predicate.
-
지정된 predicate 함수와 일치하는 마지막 요소를 반환합니다. 일치하는 요소가 없으면 기본값을 반환합니다.
const arr = [1, 2, 3, 4, 5];
const result = arr.lastOrDefault((x) => x % 2 === 0);
expect(result).toBe(4);
diff(other: T[]): T[];
const arr = [1, 2, 3, 4, 5];
const result = arr.diff([1, 5, 6]);
expect(result).toEqual([2, 3, 4]);
inter(other: T[]): T[];
const arr = [1, 2, 3, 4, 5];
const result = arr.inter([1, 3, 5, 6]);
expect(result).toEqual([1, 3, 5]);
_deepCopy(): T[];
_toJson(): string;
Extention Helper
Math
round10(x: number, point?: number)
floor10(x: number, point?: number)
ceil10(x: number, point?: number)
A function that allows you to use decimal points for discarding/rounding/round.
I used the code of the MDN below.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil#decimal_adjustment
console.log(Math.round10(112.5345, 3));
console.log(Math.floor10(112.5345, 3));
console.log(Math.ceil10(112.5345, 3));
randomRange(a: number, b: number, point?: number)
Create random numbers within that range.
(Includes maximum and minimum values.)
console.log(Math.randomRange(112.5, 200, 1));
console.log(Math.randomRange(0, 200));
clamp(input: number, min: number, max: number)
console.log(Math.clamp(10, 3, 5));
console.log(Math.clamp(1, 3, 5));
console.log(Math.clamp(4, 3, 5));