j-kompact 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +151 -0
- package/dist/Array.d.ts +110 -0
- package/dist/Array.js +298 -0
- package/dist/Boolean.d.ts +7 -0
- package/dist/Boolean.js +18 -0
- package/dist/Comparison.d.ts +22 -0
- package/dist/Comparison.js +64 -0
- package/dist/Date.d.ts +32 -0
- package/dist/Date.js +106 -0
- package/dist/Dynamic.d.ts +9 -0
- package/dist/Dynamic.js +28 -0
- package/dist/Error.d.ts +4 -0
- package/dist/Error.js +7 -0
- package/dist/Iteration.d.ts +21 -0
- package/dist/Iteration.js +51 -0
- package/dist/Manipulation.d.ts +7 -0
- package/dist/Manipulation.js +19 -0
- package/dist/Object.d.ts +58 -0
- package/dist/Object.js +161 -0
- package/dist/String.d.ts +31 -0
- package/dist/String.js +57 -0
- package/dist/Type.d.ts +3 -0
- package/dist/Type.js +3 -0
- package/dist/google/spreadsheet/Utilites.d.ts +8 -0
- package/dist/google/spreadsheet/Utilites.js +18 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/global.d.ts +10 -0
- package/package.json +33 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { And } from './Type.js';
|
|
2
|
+
import { isObject } from './Object.js';
|
|
3
|
+
import { flat, getOptions, lazyWrap, MLArray } from './Array.js';
|
|
4
|
+
import { isString } from "./String.js";
|
|
5
|
+
import { log } from './Dynamic.js';
|
|
6
|
+
export function isSame(...arr) {
|
|
7
|
+
return new Set(flat(arr)).size === 1;
|
|
8
|
+
}
|
|
9
|
+
export function sameWith(value, ...arr) {
|
|
10
|
+
const { withLog = true, logic = And } = getOptions(arr), flattenArr = arr.lazyFlat();
|
|
11
|
+
if (withLog) {
|
|
12
|
+
log(`Value: ${JSON.stringify(value)}`);
|
|
13
|
+
log(`Compare With: ${JSON.stringify(flattenArr)}`);
|
|
14
|
+
}
|
|
15
|
+
const compare = (val) => isObject(value) && isObject(val)
|
|
16
|
+
? Object.keys(val).every(key => val[key] === value[key])
|
|
17
|
+
: val === value;
|
|
18
|
+
return logic === And
|
|
19
|
+
? flattenArr.every(compare)
|
|
20
|
+
: flattenArr.some(compare);
|
|
21
|
+
}
|
|
22
|
+
export function notSameWith(value, ...arr) {
|
|
23
|
+
return !flat(arr).includes(value);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Mengecek apakah 'value' ada di antara 'start' sampai 'until'
|
|
27
|
+
* Mendukung komparasi: '<', '<=', '<=<', '<<='
|
|
28
|
+
*/
|
|
29
|
+
export function between(start, value, until, compare = '<') {
|
|
30
|
+
[start, until] = [start ?? 0, until ?? 0];
|
|
31
|
+
let values = lazyWrap(value).map((value) => value ?? 0);
|
|
32
|
+
const compares = lazyWrap(compare);
|
|
33
|
+
if (values.length !== compare.length && values.length === 1)
|
|
34
|
+
values = MLArray.repeat(values[0], compare.length);
|
|
35
|
+
return compares.every((compare, no) => {
|
|
36
|
+
const number = values[no];
|
|
37
|
+
switch (compare) {
|
|
38
|
+
case '<':
|
|
39
|
+
return (start < number) && (number < until);
|
|
40
|
+
case '<=':
|
|
41
|
+
return (start <= number) && (number <= until);
|
|
42
|
+
case '<=<':
|
|
43
|
+
return (start <= number) && (number < until);
|
|
44
|
+
case '<<=':
|
|
45
|
+
return (start < number) && (number <= until);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
export function lowerThan(value, ...comparations) {
|
|
50
|
+
const { logic = And } = getOptions(comparations), isLowerThan = (comparation) => value < comparation;
|
|
51
|
+
return logic === And
|
|
52
|
+
? flat(comparations).every(isLowerThan)
|
|
53
|
+
: flat(comparations).some(isLowerThan);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Mengecek tipe data menggunakan "typeof" bawaan JavaScript untuk banyak variabel sekaligus
|
|
57
|
+
*/
|
|
58
|
+
export function isTypeOf(type, ...array) {
|
|
59
|
+
const { logic = And } = getOptions(array);
|
|
60
|
+
array = array.lazyFlat();
|
|
61
|
+
return logic === And
|
|
62
|
+
? array.every((val) => type === 'string' ? isString(val) : typeof val === type)
|
|
63
|
+
: array.some((val) => type === 'string' ? isString(val) : typeof val === type);
|
|
64
|
+
}
|
package/dist/Date.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
declare const shortMonths: import("./Array.ts").MLArray<"AGS" | "APR" | "DES" | "FEB" | "JAN" | "JUL" | "JUN" | "MAR" | "MEI" | "NOV" | "OKT" | "SEP">, longMonths: import("./Array.ts").MLArray<"AGUSTUS" | "APRIL" | "DESEMBER" | "FEBRUARI" | "JANUARI" | "JULI" | "JUNI" | "MARET" | "MEI" | "NOVEMBER" | "OKTOBER" | "SEPTEMBER">;
|
|
2
|
+
export declare class MLDate extends Date {
|
|
3
|
+
constructor(...values: number[] | [string | Date | MLDate | DateObject]);
|
|
4
|
+
/** Getter & Setter */
|
|
5
|
+
get date(): number;
|
|
6
|
+
get lastDate(): number;
|
|
7
|
+
get month(): number;
|
|
8
|
+
get shortMonth(): import("./String.ts").MLString<string>;
|
|
9
|
+
get longMonth(): import("./String.ts").MLString<string>;
|
|
10
|
+
get year(): number;
|
|
11
|
+
set date(date: number);
|
|
12
|
+
set month(month: number | string);
|
|
13
|
+
set year(year: number);
|
|
14
|
+
setDate(date: number): MLDate;
|
|
15
|
+
setDate(date: number): number;
|
|
16
|
+
setMonth(month: number | string): MLDate;
|
|
17
|
+
setMonth(month: number, date?: number): number;
|
|
18
|
+
lastMonth(): MLDate;
|
|
19
|
+
nextMonth(): MLDate;
|
|
20
|
+
format(format: string, timezone?: string): any;
|
|
21
|
+
}
|
|
22
|
+
export type DateObject = {
|
|
23
|
+
year?: number;
|
|
24
|
+
month?: number | typeof longMonths[number] | typeof shortMonths[number];
|
|
25
|
+
date?: number;
|
|
26
|
+
};
|
|
27
|
+
export declare function initDate(...value: number[] | [string | Date | MLDate | DateObject]): MLDate;
|
|
28
|
+
/**
|
|
29
|
+
* Cek apakah string / objek Date adalah tanggal yang valid
|
|
30
|
+
*/
|
|
31
|
+
export declare function isDate(value: any): boolean;
|
|
32
|
+
export {};
|
package/dist/Date.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { initString } from './String.js';
|
|
2
|
+
import { initArray } from './Array.js';
|
|
3
|
+
import { isObject } from './Object.js';
|
|
4
|
+
import dayjs from 'dayjs';
|
|
5
|
+
import utc from 'dayjs/plugin/utc.js';
|
|
6
|
+
import tz from 'dayjs/plugin/timezone.js';
|
|
7
|
+
const shortMonths = initArray([
|
|
8
|
+
'JAN', 'FEB', 'MAR', 'APR', 'MEI', 'JUN',
|
|
9
|
+
'JUL', 'AGS', 'SEP', 'OKT', 'NOV', 'DES'
|
|
10
|
+
]), longMonths = initArray([
|
|
11
|
+
'JANUARI', 'FEBRUARI', 'MARET', 'APRIL', 'MEI', 'JUNI',
|
|
12
|
+
'JULI', 'AGUSTUS', 'SEPTEMBER', 'OKTOBER', 'NOVEMBER', 'DESEMBER'
|
|
13
|
+
]);
|
|
14
|
+
export class MLDate extends Date {
|
|
15
|
+
constructor(...values) {
|
|
16
|
+
super(...values);
|
|
17
|
+
}
|
|
18
|
+
/** Getter & Setter */
|
|
19
|
+
get date() {
|
|
20
|
+
return super.getDate();
|
|
21
|
+
}
|
|
22
|
+
get lastDate() {
|
|
23
|
+
return new Date(this.year, this.month, 0).getDate();
|
|
24
|
+
}
|
|
25
|
+
get month() {
|
|
26
|
+
return this.getMonth() + 1;
|
|
27
|
+
}
|
|
28
|
+
get shortMonth() {
|
|
29
|
+
return initString(shortMonths.get(this.month));
|
|
30
|
+
}
|
|
31
|
+
get longMonth() {
|
|
32
|
+
return initString(longMonths.get(this.month));
|
|
33
|
+
}
|
|
34
|
+
get year() {
|
|
35
|
+
return this.getFullYear();
|
|
36
|
+
}
|
|
37
|
+
set date(date) {
|
|
38
|
+
super.setDate(date);
|
|
39
|
+
}
|
|
40
|
+
set month(month) {
|
|
41
|
+
let param;
|
|
42
|
+
if (typeof month === 'string')
|
|
43
|
+
param = month.length === 3
|
|
44
|
+
? shortMonths.locationOf(month)
|
|
45
|
+
: longMonths.locationOf(month);
|
|
46
|
+
else
|
|
47
|
+
param = month;
|
|
48
|
+
super.setMonth(param - 1);
|
|
49
|
+
}
|
|
50
|
+
set year(year) {
|
|
51
|
+
super.setFullYear(year);
|
|
52
|
+
}
|
|
53
|
+
setDate(date) {
|
|
54
|
+
this.date = date;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
setMonth(month) {
|
|
58
|
+
this.month = month;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
lastMonth() {
|
|
62
|
+
return new MLDate(this.getTime()).setDate(1).setMonth(this.month - 1);
|
|
63
|
+
}
|
|
64
|
+
nextMonth() {
|
|
65
|
+
return new MLDate(this.getTime()).setDate(1).setMonth(this.month + 1);
|
|
66
|
+
}
|
|
67
|
+
format(format, timezone = 'Asia/Jakarta') {
|
|
68
|
+
if (typeof Utilities !== 'undefined' && typeof Utilities.formatDate === 'function')
|
|
69
|
+
return Utilities.formatDate(this, timezone, format);
|
|
70
|
+
dayjs.extend(utc);
|
|
71
|
+
dayjs.extend(tz);
|
|
72
|
+
return dayjs(this)
|
|
73
|
+
.tz(timezone)
|
|
74
|
+
.format(format.replaceAll('y', 'Y').replaceAll('d', 'D'));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export function initDate(...value) {
|
|
78
|
+
switch (true) {
|
|
79
|
+
case isObject(value[0]):
|
|
80
|
+
const today = new MLDate(), { date = today.date, month = today.month, year = today.year } = value[0];
|
|
81
|
+
return new MLDate(year, +(typeof month === 'string'
|
|
82
|
+
? month.length === 3
|
|
83
|
+
? shortMonths.indexOf(month)
|
|
84
|
+
: longMonths.indexOf(month)
|
|
85
|
+
: (month - 1)), date);
|
|
86
|
+
case !!value.length:
|
|
87
|
+
return new MLDate(...value);
|
|
88
|
+
case value[0] instanceof MLDate:
|
|
89
|
+
return value[0];
|
|
90
|
+
case value[0] instanceof Date:
|
|
91
|
+
return new MLDate(value[0]);
|
|
92
|
+
default:
|
|
93
|
+
return new MLDate();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Cek apakah string / objek Date adalah tanggal yang valid
|
|
98
|
+
*/
|
|
99
|
+
export function isDate(value) {
|
|
100
|
+
if (value instanceof Date)
|
|
101
|
+
return !isNaN(value.getTime());
|
|
102
|
+
if (typeof value !== 'string' || /^[a-zA-Z]+[-_]\d+/.test(value))
|
|
103
|
+
return false;
|
|
104
|
+
const date = new Date(value);
|
|
105
|
+
return !isNaN(date.getTime());
|
|
106
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File ini berisikan fungsi2 yang akan dijalankan dinamis sesuai environtment-nya
|
|
3
|
+
* Mendukung environtment Node.js dan Google Apps Script
|
|
4
|
+
*/
|
|
5
|
+
export declare function log(...args: any[]): void;
|
|
6
|
+
/**
|
|
7
|
+
* @param delay - In second
|
|
8
|
+
*/
|
|
9
|
+
export declare function wait(delay: number): void;
|
package/dist/Dynamic.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File ini berisikan fungsi2 yang akan dijalankan dinamis sesuai environtment-nya
|
|
3
|
+
* Mendukung environtment Node.js dan Google Apps Script
|
|
4
|
+
*/
|
|
5
|
+
import { isObject, MLObject } from './Object.js';
|
|
6
|
+
import { toString } from './String.js';
|
|
7
|
+
export function log(...args) {
|
|
8
|
+
if (typeof Logger !== 'undefined' && typeof Logger.log === 'function')
|
|
9
|
+
Logger.log(args.map(arg => {
|
|
10
|
+
if (arg instanceof MLObject)
|
|
11
|
+
arg = arg.object;
|
|
12
|
+
if (isObject(arg))
|
|
13
|
+
return toString(arg);
|
|
14
|
+
return String(arg);
|
|
15
|
+
}).join(' '));
|
|
16
|
+
else
|
|
17
|
+
console.log(...args);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param delay - In second
|
|
21
|
+
*/
|
|
22
|
+
export function wait(delay) {
|
|
23
|
+
delay *= 1000;
|
|
24
|
+
if (typeof Utilities !== 'undefined' && typeof Utilities.sleep === 'function')
|
|
25
|
+
Utilities.sleep(delay);
|
|
26
|
+
else
|
|
27
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay);
|
|
28
|
+
}
|
package/dist/Error.d.ts
ADDED
package/dist/Error.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { log } from './Dynamic.js';
|
|
2
|
+
export function templateLogError(e, functionName = '', options = {}) {
|
|
3
|
+
const { withThrow = true, prefixMessage = '' } = options;
|
|
4
|
+
log(`${prefixMessage ? `${prefixMessage}\n` : ''}${functionName ? `Function: ${functionName}\n` : ''}Error: ${e.message}\n${e.stack}`);
|
|
5
|
+
if (withThrow)
|
|
6
|
+
throw e;
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const Break = "BREAK";
|
|
2
|
+
/**
|
|
3
|
+
* Deklarasi perulangan for dengan formatDate seperti forEach
|
|
4
|
+
*/
|
|
5
|
+
export declare function iterate<T>(func: (no: number) => void | T, options?: {
|
|
6
|
+
from?: number;
|
|
7
|
+
until?: number;
|
|
8
|
+
untilBefore?: boolean;
|
|
9
|
+
addition?: number;
|
|
10
|
+
withReturnValue?: boolean;
|
|
11
|
+
}): (void | boolean | T)[] | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Fungsi pemanggilan API dengan skema perulangan saat gagal.
|
|
14
|
+
* Dapat disesuaikan jumlah percobaan dan durasi antar percobaannya.
|
|
15
|
+
*/
|
|
16
|
+
export declare function retry<T>(func: () => void | T, options?: {
|
|
17
|
+
attempts?: number;
|
|
18
|
+
waitingInSec?: number;
|
|
19
|
+
withReturnValue?: boolean;
|
|
20
|
+
fallback?: any;
|
|
21
|
+
}): void | T[] | any;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { log, wait } from './Dynamic.js';
|
|
2
|
+
export const Break = 'BREAK';
|
|
3
|
+
/**
|
|
4
|
+
* Deklarasi perulangan for dengan formatDate seperti forEach
|
|
5
|
+
*/
|
|
6
|
+
export function iterate(func, options = {}) {
|
|
7
|
+
let { from = 1, until = 1 } = options;
|
|
8
|
+
if (from > until && until === 1)
|
|
9
|
+
until = from;
|
|
10
|
+
const { untilBefore = false, addition = 1, withReturnValue = func((from += addition) - addition) } = options, result = [withReturnValue];
|
|
11
|
+
if (withReturnValue === Break)
|
|
12
|
+
return;
|
|
13
|
+
for (let i = from; i <= (until - +untilBefore); i += addition) {
|
|
14
|
+
const returned = func(i);
|
|
15
|
+
if (withReturnValue)
|
|
16
|
+
result.push(returned);
|
|
17
|
+
if (returned === Break)
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
if (withReturnValue)
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Fungsi pemanggilan API dengan skema perulangan saat gagal.
|
|
25
|
+
* Dapat disesuaikan jumlah percobaan dan durasi antar percobaannya.
|
|
26
|
+
*/
|
|
27
|
+
export function retry(func, options = {}) {
|
|
28
|
+
const { attempts = 3, waitingInSec = 2, withReturnValue = false, fallback = null } = options;
|
|
29
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
30
|
+
try {
|
|
31
|
+
if (withReturnValue)
|
|
32
|
+
return func();
|
|
33
|
+
else
|
|
34
|
+
func();
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
if (attempt === attempts) {
|
|
38
|
+
if (typeof fallback === 'function')
|
|
39
|
+
if (withReturnValue)
|
|
40
|
+
return fallback();
|
|
41
|
+
else
|
|
42
|
+
fallback();
|
|
43
|
+
else if (withReturnValue)
|
|
44
|
+
return fallback;
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
47
|
+
log(`Retry ${attempt}/${attempts}: ${e.message}`);
|
|
48
|
+
wait(attempt * waitingInSec);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Melakukan trim string secara menyeluruh, baik untuk string bisa, Array of String, maupun Object dengan value berupa string.
|
|
3
|
+
* Data Array / Object campuran didukung dan akan melakukan trim pada string saja.
|
|
4
|
+
*/
|
|
5
|
+
export declare function trim<T>(value: T, options?: {
|
|
6
|
+
useJsObject?: boolean;
|
|
7
|
+
}): T;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isArray } from "./Array.js";
|
|
2
|
+
import { initObject, isObject, MLObject } from "./Object.js";
|
|
3
|
+
import { isString } from "./String.js";
|
|
4
|
+
/**
|
|
5
|
+
* Melakukan trim string secara menyeluruh, baik untuk string bisa, Array of String, maupun Object dengan value berupa string.
|
|
6
|
+
* Data Array / Object campuran didukung dan akan melakukan trim pada string saja.
|
|
7
|
+
*/
|
|
8
|
+
export function trim(value, options = {}) {
|
|
9
|
+
const { useJsObject = false } = options;
|
|
10
|
+
if (isArray(value))
|
|
11
|
+
return value.map(val => trim(val, options));
|
|
12
|
+
if (isObject(value)) {
|
|
13
|
+
const result = initObject((value instanceof MLObject ? value.object : value)).reEntries((key, value) => [key, trim(value, options)]);
|
|
14
|
+
if (useJsObject)
|
|
15
|
+
return result.object;
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
return isString(value) ? value.trim() : value;
|
|
19
|
+
}
|
package/dist/Object.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type MLArray } from './Array.ts';
|
|
2
|
+
import { MLString } from './String.ts';
|
|
3
|
+
import { mlArray, mlObject } from './Type.ts';
|
|
4
|
+
type mlObjectCallbackFunction<T, U> = (key: string, value: T) => U;
|
|
5
|
+
type CustomMLClass = typeof mlObject | typeof mlArray;
|
|
6
|
+
/**
|
|
7
|
+
* ML: MASTER LIBRARY
|
|
8
|
+
*/
|
|
9
|
+
export declare class MLObject<T> {
|
|
10
|
+
#private;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
object: Record<string, T>;
|
|
13
|
+
static fromEntries<T>(entries: Iterable<readonly [PropertyKey, any]>): MLObject<any>;
|
|
14
|
+
static assign<T>(values: MLArray<Record<string, T>>): MLObject<T>;
|
|
15
|
+
constructor(object: Record<string, T>);
|
|
16
|
+
get entries(): MLArray<(string | T)[]>;
|
|
17
|
+
get keys(): MLArray<string>;
|
|
18
|
+
get values(): MLArray<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Membalikkan { key: value } di Object menjadi { value: key }
|
|
21
|
+
*/
|
|
22
|
+
reverse(): Record<string, string>;
|
|
23
|
+
reEntries<U>(callbackFunc: mlObjectCallbackFunction<T, U>): MLObject<U>;
|
|
24
|
+
forEach<U>(func: mlObjectCallbackFunction<T, U>): void;
|
|
25
|
+
map<U>(func: mlObjectCallbackFunction<T, U>): MLArray<U>;
|
|
26
|
+
filter(func: mlObjectCallbackFunction<T, boolean>): MLObject<any>;
|
|
27
|
+
/**
|
|
28
|
+
* Mendapatkan nilai berdasarkan key (bisa banyak) dari suatu object
|
|
29
|
+
*/
|
|
30
|
+
get(...keys: string[] | [...string[], {
|
|
31
|
+
isDeleteNull: boolean;
|
|
32
|
+
}] | [string[], {
|
|
33
|
+
isDeleteNull: boolean;
|
|
34
|
+
}]): T | MLArray<T>;
|
|
35
|
+
getKeyByValue(...values: T[]): MLArray<MLString<string> | undefined> | MLString<string> | undefined;
|
|
36
|
+
delete(...keys: string[]): this;
|
|
37
|
+
set(keys: string | string[] | MLArray<string> | MLObject<T>, values?: T | T[] | MLArray<T> | null): this;
|
|
38
|
+
convertValueAs(type: CustomMLClass): MLObject<(string | MLArray<T extends readonly any[] ? T[number] : T>)[]>;
|
|
39
|
+
add(otherObject: Record<string, T> | MLObject<T>): void;
|
|
40
|
+
}
|
|
41
|
+
export declare function initObject<T>(object?: Record<string, T>): MLObject<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Cek apakah sebuah nilai merupakan objek plain / MLObject, bukan array dan bukan null
|
|
44
|
+
*/
|
|
45
|
+
export declare function isObject(...values: any[]): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Cek apakah sebuah objek kosong atau tidak
|
|
48
|
+
*/
|
|
49
|
+
export declare function isEmpty<T>(object: Record<string, T>): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Parsing dari tipe data apapun ke dalam bentuk Object
|
|
52
|
+
*/
|
|
53
|
+
export declare function parse<T>(param: Record<string, T>[] | Iterable<readonly [PropertyKey, any]> | string): Record<string, T>;
|
|
54
|
+
/**
|
|
55
|
+
* Melakukan konversi MLObject ke Object native JavaScript
|
|
56
|
+
*/
|
|
57
|
+
export declare function toJSObject(value: any): Object;
|
|
58
|
+
export {};
|
package/dist/Object.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { getOptions, initArray, isAllArray, isArray } from './Array.js';
|
|
2
|
+
import { initString, MLString } from './String.js';
|
|
3
|
+
import { mlArray, mlObject } from './Type.js';
|
|
4
|
+
import { log } from './Dynamic.js';
|
|
5
|
+
/**
|
|
6
|
+
* ML: MASTER LIBRARY
|
|
7
|
+
*/
|
|
8
|
+
export class MLObject {
|
|
9
|
+
object = {};
|
|
10
|
+
#entriesVersion;
|
|
11
|
+
#keysVersion = initArray();
|
|
12
|
+
#valuesVersion = initArray();
|
|
13
|
+
#reversedObject;
|
|
14
|
+
static fromEntries(entries) {
|
|
15
|
+
return new this(Object.fromEntries(entries));
|
|
16
|
+
}
|
|
17
|
+
static assign(values) {
|
|
18
|
+
const result = {};
|
|
19
|
+
values.iterate((data) => Object.assign(result, data));
|
|
20
|
+
return new this(result);
|
|
21
|
+
}
|
|
22
|
+
constructor(object) {
|
|
23
|
+
this.add(object);
|
|
24
|
+
}
|
|
25
|
+
get entries() {
|
|
26
|
+
if (!this.#entriesVersion)
|
|
27
|
+
this.#entriesVersion = initArray(Object.entries(this.object));
|
|
28
|
+
return this.#entriesVersion;
|
|
29
|
+
}
|
|
30
|
+
get keys() {
|
|
31
|
+
if (!this.#keysVersion?.length)
|
|
32
|
+
this.#keysVersion = initArray(Object.keys(this.object));
|
|
33
|
+
return this.#keysVersion;
|
|
34
|
+
}
|
|
35
|
+
get values() {
|
|
36
|
+
if (!this.#valuesVersion.length)
|
|
37
|
+
this.#valuesVersion = initArray(Object.values(this.object));
|
|
38
|
+
return this.#valuesVersion;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Membalikkan { key: value } di Object menjadi { value: key }
|
|
42
|
+
*/
|
|
43
|
+
reverse() {
|
|
44
|
+
if (!this.#reversedObject)
|
|
45
|
+
this.#reversedObject = this.reEntries((key, value) => [value, key]);
|
|
46
|
+
return this.#reversedObject;
|
|
47
|
+
}
|
|
48
|
+
reEntries(callbackFunc) {
|
|
49
|
+
return MLObject.fromEntries(this.map(callbackFunc));
|
|
50
|
+
}
|
|
51
|
+
forEach(func) {
|
|
52
|
+
this.entries.forEach(([key, value]) => func(key, value));
|
|
53
|
+
}
|
|
54
|
+
map(func) {
|
|
55
|
+
return this.entries.map(([key, value]) => func(key, value));
|
|
56
|
+
}
|
|
57
|
+
filter(func) {
|
|
58
|
+
return MLObject.fromEntries(this.entries.immutableFilter(([key, value]) => func(key, value)));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Mendapatkan nilai berdasarkan key (bisa banyak) dari suatu object
|
|
62
|
+
*/
|
|
63
|
+
get(...keys) {
|
|
64
|
+
const { isDeleteNull = false } = getOptions(keys), mLArrayKeys = initArray(keys, { flatting: true }), process = (key) => this[key];
|
|
65
|
+
if (mLArrayKeys.length > 1) {
|
|
66
|
+
const result = mLArrayKeys.map(process);
|
|
67
|
+
return isDeleteNull
|
|
68
|
+
? result.deleteNull()
|
|
69
|
+
: result;
|
|
70
|
+
}
|
|
71
|
+
return process(mLArrayKeys[0]);
|
|
72
|
+
}
|
|
73
|
+
getKeyByValue(...values) {
|
|
74
|
+
const mlArrayValues = initArray(values, { flatting: true }), result = mlArrayValues.map(value => {
|
|
75
|
+
let result = this.reverse()[value];
|
|
76
|
+
if (typeof result === 'string')
|
|
77
|
+
result = initString(result);
|
|
78
|
+
return result;
|
|
79
|
+
});
|
|
80
|
+
return result.length > 1 ? result : result[0];
|
|
81
|
+
}
|
|
82
|
+
delete(...keys) {
|
|
83
|
+
initArray(keys, { flatting: true })
|
|
84
|
+
.iterate((key) => {
|
|
85
|
+
delete this.object[key];
|
|
86
|
+
delete this[key];
|
|
87
|
+
});
|
|
88
|
+
return this.#reset();
|
|
89
|
+
}
|
|
90
|
+
#reset() {
|
|
91
|
+
this.#entriesVersion = undefined;
|
|
92
|
+
this.#keysVersion = initArray();
|
|
93
|
+
this.#valuesVersion = initArray();
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
set(keys, values = null) {
|
|
97
|
+
if (isObject(keys))
|
|
98
|
+
initObject(keys).forEach((key, value) => this.object[key] = this[key] = value);
|
|
99
|
+
else {
|
|
100
|
+
const [keysWrapped, valuesWrapped] = [initArray(keys), initArray(values)];
|
|
101
|
+
keysWrapped.iterate((data, no) => this.object[data] = this[data] = valuesWrapped[no]);
|
|
102
|
+
}
|
|
103
|
+
return this.#reset();
|
|
104
|
+
}
|
|
105
|
+
convertValueAs(type) {
|
|
106
|
+
switch (type) {
|
|
107
|
+
case mlArray:
|
|
108
|
+
return this.reEntries((key, value) => [key, initArray(value)]);
|
|
109
|
+
case mlObject:
|
|
110
|
+
return this.reEntries((key, value) => [key, initObject(value)]);
|
|
111
|
+
default:
|
|
112
|
+
log(`Tipe ${type} tidak valid`);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
add(otherObject) {
|
|
117
|
+
if (otherObject instanceof MLObject)
|
|
118
|
+
otherObject = otherObject.object;
|
|
119
|
+
this.object = otherObject;
|
|
120
|
+
this.#reset();
|
|
121
|
+
this.forEach((key, value) => {
|
|
122
|
+
if (!(key in this))
|
|
123
|
+
this[key] = value;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export function initObject(object = {}) {
|
|
128
|
+
return new MLObject(object);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Cek apakah sebuah nilai merupakan objek plain / MLObject, bukan array dan bukan null
|
|
132
|
+
*/
|
|
133
|
+
export function isObject(...values) {
|
|
134
|
+
return values.every(value => value && (value.constructor.toString().startsWith('function Object()') || value instanceof MLObject) && !isArray(value));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Cek apakah sebuah objek kosong atau tidak
|
|
138
|
+
*/
|
|
139
|
+
export function isEmpty(object) {
|
|
140
|
+
return !(!!Object.keys(object).length);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Parsing dari tipe data apapun ke dalam bentuk Object
|
|
144
|
+
*/
|
|
145
|
+
export function parse(param) {
|
|
146
|
+
if (typeof param === 'string')
|
|
147
|
+
return JSON.parse(param);
|
|
148
|
+
if (isAllArray(...param))
|
|
149
|
+
return Object.fromEntries(param);
|
|
150
|
+
return Object.assign({}, ...param);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Melakukan konversi MLObject ke Object native JavaScript
|
|
154
|
+
*/
|
|
155
|
+
export function toJSObject(value) {
|
|
156
|
+
if (isArray(value))
|
|
157
|
+
return value.map(toJSObject);
|
|
158
|
+
if (value instanceof MLObject)
|
|
159
|
+
return value.object;
|
|
160
|
+
return value;
|
|
161
|
+
}
|
package/dist/String.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { And, Or } from './Type.ts';
|
|
2
|
+
declare const Capitalize = "capitalize", LowerCase = "lowercase", UpperCase = "uppercase";
|
|
3
|
+
type NormalizeMode = typeof Capitalize | typeof LowerCase | typeof UpperCase;
|
|
4
|
+
type Logic = typeof And | typeof Or;
|
|
5
|
+
export declare class MLString<T extends string = string> extends String {
|
|
6
|
+
readonly _literalType: T;
|
|
7
|
+
toJSON(): string;
|
|
8
|
+
toCamelCase(): MLString<string>;
|
|
9
|
+
normalizeFromCamelCase(mode?: NormalizeMode): "" | MLString<string>;
|
|
10
|
+
capitalize(): "" | MLString<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Mengecek apakah suatu teks ada di dalam teks-teks lain
|
|
13
|
+
*/
|
|
14
|
+
includes(...searchValues: string[] | {
|
|
15
|
+
logic?: Logic;
|
|
16
|
+
}[]): boolean;
|
|
17
|
+
includes(string: string, position?: number): boolean;
|
|
18
|
+
endWith(...searchValues: string[] | [{
|
|
19
|
+
logic?: Logic;
|
|
20
|
+
caseInsensitive?: boolean;
|
|
21
|
+
}]): boolean;
|
|
22
|
+
formatNumber(digitCount?: number): string;
|
|
23
|
+
}
|
|
24
|
+
export declare function initString<const T = string>(value: T): MLString<T extends string ? T : string>;
|
|
25
|
+
export declare const toString: {
|
|
26
|
+
(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
|
|
27
|
+
(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
|
|
28
|
+
};
|
|
29
|
+
export declare function isString(value: any): value is string | String;
|
|
30
|
+
export declare function formatNumber(number: number, digitCount?: number): string;
|
|
31
|
+
export {};
|
package/dist/String.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { And, Or } from './Type.js';
|
|
2
|
+
import { getOptions } from './Array.js';
|
|
3
|
+
const Capitalize = 'capitalize', LowerCase = 'lowercase', UpperCase = 'uppercase';
|
|
4
|
+
export class MLString extends String {
|
|
5
|
+
_literalType;
|
|
6
|
+
toJSON() {
|
|
7
|
+
return this.toString();
|
|
8
|
+
}
|
|
9
|
+
toCamelCase() {
|
|
10
|
+
return initString(this
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.replace(/[^a-zA-Z0-9]+(.)/g, (_, char) => char.toUpperCase())
|
|
13
|
+
.replace(/^[A-Z]/, (char) => char.toLowerCase())
|
|
14
|
+
.replace(/\W+/, ''));
|
|
15
|
+
}
|
|
16
|
+
normalizeFromCamelCase(mode = Capitalize) {
|
|
17
|
+
const result = this.replace(/[A-Z]/g, ' $&');
|
|
18
|
+
switch (mode) {
|
|
19
|
+
case Capitalize:
|
|
20
|
+
return initString(result).capitalize();
|
|
21
|
+
case LowerCase:
|
|
22
|
+
return initString(result.toLowerCase());
|
|
23
|
+
case UpperCase:
|
|
24
|
+
return initString(result.toUpperCase());
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
capitalize() {
|
|
28
|
+
return this.length ? initString(this[0].toUpperCase() + this.slice(1).toLowerCase()) : '';
|
|
29
|
+
}
|
|
30
|
+
includes(...searchValues) {
|
|
31
|
+
const { logic = And } = getOptions(searchValues), process = (searchValue) => super.includes(searchValue);
|
|
32
|
+
searchValues = searchValues.lazyFlat();
|
|
33
|
+
return logic === And
|
|
34
|
+
? searchValues.every(process)
|
|
35
|
+
: searchValues.some(process);
|
|
36
|
+
}
|
|
37
|
+
endWith(...searchValues) {
|
|
38
|
+
const { logic = And, caseInsensitive = false } = getOptions(searchValues), process = (searchValue) => caseInsensitive ? this.toLowerCase().endsWith(searchValue.toLowerCase()) : this.endsWith(searchValue);
|
|
39
|
+
const flattenSearchValues = searchValues.lazyFlat();
|
|
40
|
+
return logic === And
|
|
41
|
+
? flattenSearchValues.every(process)
|
|
42
|
+
: flattenSearchValues.some(process);
|
|
43
|
+
}
|
|
44
|
+
formatNumber(digitCount = 2) {
|
|
45
|
+
return this.padStart(digitCount, '0');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function initString(value) {
|
|
49
|
+
return new MLString(value);
|
|
50
|
+
}
|
|
51
|
+
export const toString = JSON.stringify;
|
|
52
|
+
export function isString(value) {
|
|
53
|
+
return typeof value === 'string' || value instanceof String;
|
|
54
|
+
}
|
|
55
|
+
export function formatNumber(number, digitCount = 2) {
|
|
56
|
+
return initString(number).formatNumber(digitCount);
|
|
57
|
+
}
|
package/dist/Type.d.ts
ADDED
package/dist/Type.js
ADDED