@okam/core-lib 0.0.1
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/index.d.ts +9 -0
- package/index.js +1 -0
- package/index.mjs +102 -0
- package/package.json +21 -0
- package/utils/array-utils.d.ts +4 -0
- package/utils/asserts.d.ts +9 -0
- package/utils/createContext.d.ts +8 -0
- package/utils/object-find-deep-nested.d.ts +2 -0
- package/utils/object-property.d.ts +2 -0
- package/utils/random-utils.d.ts +8 -0
- package/utils/type-utils.d.ts +1 -0
- package/utils/typeguards.d.ts +8 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Asserts } from './utils/asserts';
|
|
2
|
+
export { ArrayUtils } from './utils/array-utils';
|
|
3
|
+
export * from './utils/typeguards';
|
|
4
|
+
export type { UnPromisify } from './utils/type-utils';
|
|
5
|
+
export declare const sayHello: (name: string) => string;
|
|
6
|
+
export { default as createCtx } from './utils/createContext';
|
|
7
|
+
export { createCtxNullable } from './utils/createContext';
|
|
8
|
+
export { default as getNestedObjectValueOfKey } from './utils/object-find-deep-nested';
|
|
9
|
+
export { default as checkObjectProperty } from './utils/object-property';
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("react"),b=e=>{if(typeof e!="string"||!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(e))return!1;try{return new Date(e).toISOString()===e}catch{return!1}},c=(e,t=!0)=>typeof e=="string"&&(t?e.trim():e).length>0,d=e=>typeof e=="object"&&e!==null&&e.constructor===Object&&Object.getPrototypeOf(e)===Object.prototype,a=e=>typeof e=="number"&&Number.isSafeInteger(e),p=e=>typeof e=="number"&&!Number.isNaN(e)?!0:c(e)?!Number.isNaN(Number.parseInt(e,10)||Number.isNaN(Number.parseFloat(e))):!1,g=e=>{const t=typeof e=="string"&&/^-?\d+$/.test(e)?Number.parseInt(e,10):e;return a(t)},y=e=>a(e)&&e<600&&e>=100;class o{static isPresent(t,r){if(t==null)throw o.createException(r,"Value is null or undefined.")}static safeInteger(t,r){if(typeof t!="number"||!Number.isSafeInteger(t))throw o.createException(r,"Value is not a safe integer")}static nonEmptyString(t,r,n){if(!c(t,n??!0))throw o.createException(r)}static never(t,r){throw new Error(r??"Unexpected value")}static createException(t,r){throw typeof t=="string"||t===void 0?new Error(t??r??"Assertion did not pass."):t()}}function h(e,t){if([e,t].forEach((r,n)=>{if(!Number.isSafeInteger(r))throw new Error(`${n===0?"min":"max"} is not a valid integer`)}),t<e)throw new Error("Min cannot be greater than max");return e=Math.ceil(e),t=Math.floor(t),Math.floor(Math.random()*(t-e+1))+e}class N{static getRandom(t){return t[h(0,t.length-1)]}static removeItem(t,r){const n=t.indexOf(r);return n>-1&&t.splice(n,1),t}}function m(){const e=i.createContext(void 0);function t(){const r=i.useContext(e);if(r===void 0)throw new Error("useCtx must be inside a Provider");return r}return[t,e.Provider]}function x(){const e=i.createContext(void 0);function t(){const r=i.useContext(e);return r===void 0?null:r}return[t,e.Provider]}function O(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function f(e,t,r){const n=Object.keys(e);let s;s||=r;for(const u of n){const l=e[u];if(O(l)&&(s||=f(l,t,s)),u===t)return e[t]}return s}const w=(e,t)=>{const r=e[t];return r!=null&&typeof r=="object"&&Object.prototype.hasOwnProperty.call(e,t)?r:null},I=e=>`I'm the @okam/shared-ui component telling ${e} !`;exports.ArrayUtils=N;exports.Asserts=o;exports.checkObjectProperty=w;exports.createCtx=m;exports.createCtxNullable=x;exports.getNestedObjectValueOfKey=f;exports.isHttpStatusCode=y;exports.isIsoDateString=b;exports.isNonEmptyString=c;exports.isParsableNumeric=p;exports.isParsableSafeInteger=g;exports.isPlainObject=d;exports.isSafeInteger=a;exports.sayHello=I;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import s from "react";
|
|
2
|
+
const h = (t) => {
|
|
3
|
+
if (typeof t != "string" || !/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(t))
|
|
4
|
+
return !1;
|
|
5
|
+
try {
|
|
6
|
+
return new Date(t).toISOString() === t;
|
|
7
|
+
} catch {
|
|
8
|
+
return !1;
|
|
9
|
+
}
|
|
10
|
+
}, a = (t, e = !0) => typeof t == "string" && (e ? t.trim() : t).length > 0, g = (t) => typeof t == "object" && t !== null && t.constructor === Object && Object.getPrototypeOf(t) === Object.prototype, f = (t) => typeof t == "number" && Number.isSafeInteger(t), y = (t) => typeof t == "number" && !Number.isNaN(t) ? !0 : a(t) ? !Number.isNaN(Number.parseInt(t, 10) || Number.isNaN(Number.parseFloat(t))) : !1, N = (t) => {
|
|
11
|
+
const e = typeof t == "string" && /^-?\d+$/.test(t) ? Number.parseInt(t, 10) : t;
|
|
12
|
+
return f(e);
|
|
13
|
+
}, m = (t) => f(t) && t < 600 && t >= 100;
|
|
14
|
+
class i {
|
|
15
|
+
static isPresent(e, r) {
|
|
16
|
+
if (e == null)
|
|
17
|
+
throw i.createException(r, "Value is null or undefined.");
|
|
18
|
+
}
|
|
19
|
+
static safeInteger(e, r) {
|
|
20
|
+
if (typeof e != "number" || !Number.isSafeInteger(e))
|
|
21
|
+
throw i.createException(r, "Value is not a safe integer");
|
|
22
|
+
}
|
|
23
|
+
static nonEmptyString(e, r, n) {
|
|
24
|
+
if (!a(e, n ?? !0))
|
|
25
|
+
throw i.createException(r);
|
|
26
|
+
}
|
|
27
|
+
static never(e, r) {
|
|
28
|
+
throw new Error(r ?? "Unexpected value");
|
|
29
|
+
}
|
|
30
|
+
static createException(e, r) {
|
|
31
|
+
throw typeof e == "string" || e === void 0 ? new Error(e ?? r ?? "Assertion did not pass.") : e();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function l(t, e) {
|
|
35
|
+
if ([t, e].forEach((r, n) => {
|
|
36
|
+
if (!Number.isSafeInteger(r))
|
|
37
|
+
throw new Error(`${n === 0 ? "min" : "max"} is not a valid integer`);
|
|
38
|
+
}), e < t)
|
|
39
|
+
throw new Error("Min cannot be greater than max");
|
|
40
|
+
return t = Math.ceil(t), e = Math.floor(e), Math.floor(Math.random() * (e - t + 1)) + t;
|
|
41
|
+
}
|
|
42
|
+
class x {
|
|
43
|
+
static getRandom(e) {
|
|
44
|
+
return e[l(0, e.length - 1)];
|
|
45
|
+
}
|
|
46
|
+
static removeItem(e, r) {
|
|
47
|
+
const n = e.indexOf(r);
|
|
48
|
+
return n > -1 && e.splice(n, 1), e;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function w() {
|
|
52
|
+
const t = s.createContext(void 0);
|
|
53
|
+
function e() {
|
|
54
|
+
const r = s.useContext(t);
|
|
55
|
+
if (r === void 0)
|
|
56
|
+
throw new Error("useCtx must be inside a Provider");
|
|
57
|
+
return r;
|
|
58
|
+
}
|
|
59
|
+
return [e, t.Provider];
|
|
60
|
+
}
|
|
61
|
+
function I() {
|
|
62
|
+
const t = s.createContext(void 0);
|
|
63
|
+
function e() {
|
|
64
|
+
const r = s.useContext(t);
|
|
65
|
+
return r === void 0 ? null : r;
|
|
66
|
+
}
|
|
67
|
+
return [e, t.Provider];
|
|
68
|
+
}
|
|
69
|
+
function d(t) {
|
|
70
|
+
return typeof t == "object" && t !== null && !Array.isArray(t);
|
|
71
|
+
}
|
|
72
|
+
function p(t, e, r) {
|
|
73
|
+
const n = Object.keys(t);
|
|
74
|
+
let o;
|
|
75
|
+
o ||= r;
|
|
76
|
+
for (const c of n) {
|
|
77
|
+
const u = t[c];
|
|
78
|
+
if (d(u) && (o ||= p(u, e, o)), c === e)
|
|
79
|
+
return t[e];
|
|
80
|
+
}
|
|
81
|
+
return o;
|
|
82
|
+
}
|
|
83
|
+
const O = (t, e) => {
|
|
84
|
+
const r = t[e];
|
|
85
|
+
return r != null && typeof r == "object" && Object.prototype.hasOwnProperty.call(t, e) ? r : null;
|
|
86
|
+
}, j = (t) => `I'm the @okam/shared-ui component telling ${t} !`;
|
|
87
|
+
export {
|
|
88
|
+
x as ArrayUtils,
|
|
89
|
+
i as Asserts,
|
|
90
|
+
O as checkObjectProperty,
|
|
91
|
+
w as createCtx,
|
|
92
|
+
I as createCtxNullable,
|
|
93
|
+
p as getNestedObjectValueOfKey,
|
|
94
|
+
m as isHttpStatusCode,
|
|
95
|
+
h as isIsoDateString,
|
|
96
|
+
a as isNonEmptyString,
|
|
97
|
+
y as isParsableNumeric,
|
|
98
|
+
N as isParsableSafeInteger,
|
|
99
|
+
g as isPlainObject,
|
|
100
|
+
f as isSafeInteger,
|
|
101
|
+
j as sayHello
|
|
102
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@okam/core-lib",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./index.js",
|
|
5
|
+
"types": "./index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./index.mjs",
|
|
9
|
+
"require": "./index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"registry": "https://registry.npmjs.org"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"url": "https://github.com/OKAMca/stack.git"
|
|
17
|
+
},
|
|
18
|
+
"nxrelease": {
|
|
19
|
+
"repositoryUrl": "https://github.com/OKAMca/stack.git"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class Asserts {
|
|
2
|
+
static isPresent<T>(v: T, msgOrErrorFactory?: string | (() => Error)): asserts v is NonNullable<T>;
|
|
3
|
+
static safeInteger(v: unknown, msgOrErrorFactory?: string | (() => Error)): asserts v is number;
|
|
4
|
+
static nonEmptyString(v: unknown, msgOrErrorFactory?: string | (() => Error),
|
|
5
|
+
/** auto-trim, default true */
|
|
6
|
+
trim?: boolean): asserts v is string;
|
|
7
|
+
static never(_v: never, msg?: string): never;
|
|
8
|
+
private static createException;
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* A helper to create a Context and Provider with no upfront default value, and
|
|
4
|
+
* without having to check for undefined all the time.
|
|
5
|
+
*/
|
|
6
|
+
declare function createCtx<A extends Record<string, unknown> | null>(): readonly [() => A, React.Provider<A | undefined>];
|
|
7
|
+
export declare function createCtxNullable<A extends Record<string, unknown> | null>(): readonly [() => A | null, React.Provider<A | undefined>];
|
|
8
|
+
export default createCtx;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random integer between min (inclusive) and max (inclusive).
|
|
3
|
+
* The value is no lower than min (or the next integer greater than min
|
|
4
|
+
* if min isn't an integer) and no greater than max (or the next integer
|
|
5
|
+
* lower than max if max isn't an integer).
|
|
6
|
+
* @link https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range/1527820#1527820
|
|
7
|
+
*/
|
|
8
|
+
export declare function getRandomInt(min: number, max: number): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type UnPromisify<T> = T extends Promise<infer U> ? U : T;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type IsoDateString = string;
|
|
2
|
+
export declare const isIsoDateString: (dateStr: unknown) => dateStr is string;
|
|
3
|
+
export declare const isNonEmptyString: (v: unknown, trim?: boolean) => v is string;
|
|
4
|
+
export declare const isPlainObject: <T = unknown, K extends string | number = string>(v: unknown) => v is Record<K, T>;
|
|
5
|
+
export declare const isSafeInteger: (v: unknown) => v is number;
|
|
6
|
+
export declare const isParsableNumeric: (v: unknown) => v is string | number;
|
|
7
|
+
export declare const isParsableSafeInteger: (v: unknown) => v is string | number;
|
|
8
|
+
export declare const isHttpStatusCode: (v: unknown) => v is number;
|