@jeniex/utils 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/browser/index.cjs +53 -0
- package/browser/index.d.ts +17 -0
- package/browser/index.mjs +45 -0
- package/node/index.cjs +59 -0
- package/node/index.d.ts +18 -0
- package/node/index.mjs +52 -0
- package/package.json +41 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
function isString(object) {
|
|
6
|
+
return typeof object === 'string';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function ensureJoin(object, separator = ',') {
|
|
10
|
+
if (isString(object)) {
|
|
11
|
+
return object;
|
|
12
|
+
}
|
|
13
|
+
return object.join(separator);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function asserted(object) {
|
|
17
|
+
if (object === null || object === undefined) {
|
|
18
|
+
throw new Error('Value is either null or undefined');
|
|
19
|
+
}
|
|
20
|
+
return object;
|
|
21
|
+
}
|
|
22
|
+
function isTruthy(object) {
|
|
23
|
+
return Boolean(object);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function sleep(milliSeconds) {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
setTimeout(resolve, milliSeconds);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function $(selectors, parent) {
|
|
33
|
+
const element = (parent ?? document).querySelector(ensureJoin(selectors));
|
|
34
|
+
if (element === null) {
|
|
35
|
+
throw new Error(`Couldn't find the element with the selector ${selectors}`);
|
|
36
|
+
}
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
function $$(selectors, parent) {
|
|
40
|
+
const elements = (parent ?? document).querySelectorAll(ensureJoin(selectors));
|
|
41
|
+
if (elements.length === 0) {
|
|
42
|
+
throw new Error(`Couldn't find any element with the selector ${selectors}`);
|
|
43
|
+
}
|
|
44
|
+
return elements;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exports.$ = $;
|
|
48
|
+
exports.$$ = $$;
|
|
49
|
+
exports.asserted = asserted;
|
|
50
|
+
exports.ensureJoin = ensureJoin;
|
|
51
|
+
exports.isString = isString;
|
|
52
|
+
exports.isTruthy = isTruthy;
|
|
53
|
+
exports.sleep = sleep;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare function ensureJoin(object: string[] | string, separator?: string): string;
|
|
2
|
+
|
|
3
|
+
declare function asserted<T>(object: T | null | undefined): T;
|
|
4
|
+
declare function isTruthy<T>(object: T): object is NonNullable<T>;
|
|
5
|
+
|
|
6
|
+
declare function isString(object: unknown): object is string;
|
|
7
|
+
|
|
8
|
+
declare function sleep(milliSeconds: number): Promise<void>;
|
|
9
|
+
|
|
10
|
+
type Nullable<T> = T | null | undefined;
|
|
11
|
+
|
|
12
|
+
type Parent = Document | HTMLElement;
|
|
13
|
+
type Selectors = string[] | string;
|
|
14
|
+
declare function $<T = HTMLElement>(selectors: Selectors, parent?: Parent): T;
|
|
15
|
+
declare function $$<T extends Element = HTMLElement>(selectors: Selectors, parent?: Parent): NodeListOf<T>;
|
|
16
|
+
|
|
17
|
+
export { $, $$, Nullable, asserted, ensureJoin, isString, isTruthy, sleep };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
function isString(object) {
|
|
4
|
+
return typeof object === 'string';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function ensureJoin(object, separator = ',') {
|
|
8
|
+
if (isString(object)) {
|
|
9
|
+
return object;
|
|
10
|
+
}
|
|
11
|
+
return object.join(separator);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function asserted(object) {
|
|
15
|
+
if (object === null || object === undefined) {
|
|
16
|
+
throw new Error('Value is either null or undefined');
|
|
17
|
+
}
|
|
18
|
+
return object;
|
|
19
|
+
}
|
|
20
|
+
function isTruthy(object) {
|
|
21
|
+
return Boolean(object);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function sleep(milliSeconds) {
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
setTimeout(resolve, milliSeconds);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function $(selectors, parent) {
|
|
31
|
+
const element = (parent ?? document).querySelector(ensureJoin(selectors));
|
|
32
|
+
if (element === null) {
|
|
33
|
+
throw new Error(`Couldn't find the element with the selector ${selectors}`);
|
|
34
|
+
}
|
|
35
|
+
return element;
|
|
36
|
+
}
|
|
37
|
+
function $$(selectors, parent) {
|
|
38
|
+
const elements = (parent ?? document).querySelectorAll(ensureJoin(selectors));
|
|
39
|
+
if (elements.length === 0) {
|
|
40
|
+
throw new Error(`Couldn't find any element with the selector ${selectors}`);
|
|
41
|
+
}
|
|
42
|
+
return elements;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { $, $$, asserted, ensureJoin, isString, isTruthy, sleep };
|
package/node/index.cjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
var fs = require('node:fs');
|
|
6
|
+
var fsp = require('node:fs/promises');
|
|
7
|
+
var path = require('node:path');
|
|
8
|
+
|
|
9
|
+
function isString(object) {
|
|
10
|
+
return typeof object === 'string';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function ensureJoin(object, separator = ',') {
|
|
14
|
+
if (isString(object)) {
|
|
15
|
+
return object;
|
|
16
|
+
}
|
|
17
|
+
return object.join(separator);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function asserted(object) {
|
|
21
|
+
if (object === null || object === undefined) {
|
|
22
|
+
throw new Error('Value is either null or undefined');
|
|
23
|
+
}
|
|
24
|
+
return object;
|
|
25
|
+
}
|
|
26
|
+
function isTruthy(object) {
|
|
27
|
+
return Boolean(object);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function sleep(milliSeconds) {
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
setTimeout(resolve, milliSeconds);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function listFolders({ folderPath, getFullPath }) {
|
|
37
|
+
if (fs.existsSync(folderPath) === false) {
|
|
38
|
+
throw new Error('The provided folder path does not exist!');
|
|
39
|
+
}
|
|
40
|
+
const items = await fsp.readdir(folderPath, {
|
|
41
|
+
withFileTypes: true,
|
|
42
|
+
});
|
|
43
|
+
const folders = items
|
|
44
|
+
.filter((item) => item.isDirectory() === true)
|
|
45
|
+
.map((folderItem) => {
|
|
46
|
+
if (getFullPath === undefined) {
|
|
47
|
+
return folderItem.name;
|
|
48
|
+
}
|
|
49
|
+
return path.join(folderPath, folderItem.name);
|
|
50
|
+
});
|
|
51
|
+
return folders;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.asserted = asserted;
|
|
55
|
+
exports.ensureJoin = ensureJoin;
|
|
56
|
+
exports.isString = isString;
|
|
57
|
+
exports.isTruthy = isTruthy;
|
|
58
|
+
exports.listFolders = listFolders;
|
|
59
|
+
exports.sleep = sleep;
|
package/node/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare function ensureJoin(object: string[] | string, separator?: string): string;
|
|
2
|
+
|
|
3
|
+
declare function asserted<T>(object: T | null | undefined): T;
|
|
4
|
+
declare function isTruthy<T>(object: T): object is NonNullable<T>;
|
|
5
|
+
|
|
6
|
+
declare function isString(object: unknown): object is string;
|
|
7
|
+
|
|
8
|
+
declare function sleep(milliSeconds: number): Promise<void>;
|
|
9
|
+
|
|
10
|
+
type Nullable<T> = T | null | undefined;
|
|
11
|
+
|
|
12
|
+
interface ListFoldersOpt {
|
|
13
|
+
folderPath: string;
|
|
14
|
+
getFullPath?: boolean;
|
|
15
|
+
}
|
|
16
|
+
declare function listFolders({ folderPath, getFullPath }: ListFoldersOpt): Promise<string[]>;
|
|
17
|
+
|
|
18
|
+
export { Nullable, asserted, ensureJoin, isString, isTruthy, listFolders, sleep };
|
package/node/index.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import fsp from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
function isString(object) {
|
|
8
|
+
return typeof object === 'string';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function ensureJoin(object, separator = ',') {
|
|
12
|
+
if (isString(object)) {
|
|
13
|
+
return object;
|
|
14
|
+
}
|
|
15
|
+
return object.join(separator);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function asserted(object) {
|
|
19
|
+
if (object === null || object === undefined) {
|
|
20
|
+
throw new Error('Value is either null or undefined');
|
|
21
|
+
}
|
|
22
|
+
return object;
|
|
23
|
+
}
|
|
24
|
+
function isTruthy(object) {
|
|
25
|
+
return Boolean(object);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function sleep(milliSeconds) {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
setTimeout(resolve, milliSeconds);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function listFolders({ folderPath, getFullPath }) {
|
|
35
|
+
if (fs.existsSync(folderPath) === false) {
|
|
36
|
+
throw new Error('The provided folder path does not exist!');
|
|
37
|
+
}
|
|
38
|
+
const items = await fsp.readdir(folderPath, {
|
|
39
|
+
withFileTypes: true,
|
|
40
|
+
});
|
|
41
|
+
const folders = items
|
|
42
|
+
.filter((item) => item.isDirectory() === true)
|
|
43
|
+
.map((folderItem) => {
|
|
44
|
+
if (getFullPath === undefined) {
|
|
45
|
+
return folderItem.name;
|
|
46
|
+
}
|
|
47
|
+
return path.join(folderPath, folderItem.name);
|
|
48
|
+
});
|
|
49
|
+
return folders;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { asserted, ensureJoin, isString, isTruthy, listFolders, sleep };
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jeniex/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "JenieX",
|
|
5
|
+
"homepage": "https://github.com/JenieX/utils",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"utils"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
"./browser": {
|
|
12
|
+
"types": "./browser/index.d.ts",
|
|
13
|
+
"import": "./browser/index.mjs",
|
|
14
|
+
"require": "./browser/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./node": {
|
|
17
|
+
"types": "./node/index.d.ts",
|
|
18
|
+
"import": "./node/index.mjs",
|
|
19
|
+
"require": "./node/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"browser",
|
|
24
|
+
"node"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "rollup -c",
|
|
28
|
+
"develop": "npm run build -- --watch"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@jeniex/eslint-config": "^0.0.3",
|
|
32
|
+
"@rollup/plugin-typescript": "^11.1.3",
|
|
33
|
+
"@types/node": "^20.5.7",
|
|
34
|
+
"rollup": "^3.28.0",
|
|
35
|
+
"rollup-plugin-dts": "^5.3.1",
|
|
36
|
+
"tslib": "^2.6.2"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|