@pdg/data 1.0.5 → 1.0.7
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/dist/index.d.ts +3 -0
- package/dist/index.esm.js +92 -1
- package/dist/index.js +92 -1
- package/dist/makeConst.d.ts +57 -0
- package/package.json +5 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
export * from './copy';
|
|
2
2
|
export * from './lv';
|
|
3
3
|
export * from './vl';
|
|
4
|
+
export * from './makeConst';
|
|
4
5
|
import copy from './copy';
|
|
5
6
|
import lv from './lv';
|
|
6
7
|
import vl from './vl';
|
|
8
|
+
import makeConst from './makeConst';
|
|
7
9
|
declare const _default: {
|
|
8
10
|
copy: typeof copy;
|
|
9
11
|
lv: typeof lv;
|
|
10
12
|
vl: typeof vl;
|
|
13
|
+
makeConst: typeof makeConst;
|
|
11
14
|
};
|
|
12
15
|
export default _default;
|
package/dist/index.esm.js
CHANGED
|
@@ -15,8 +15,99 @@ function lv(label, value, other) {
|
|
|
15
15
|
* ******************************************************************************************************************/
|
|
16
16
|
function vl(value, label, other) {
|
|
17
17
|
return Object.assign(Object.assign({}, other), { value, label });
|
|
18
|
+
}/******************************************************************************
|
|
19
|
+
Copyright (c) Microsoft Corporation.
|
|
20
|
+
|
|
21
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
22
|
+
purpose with or without fee is hereby granted.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
25
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
26
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
27
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
28
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
29
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
30
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
31
|
+
***************************************************************************** */
|
|
32
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
function __rest(s, e) {
|
|
36
|
+
var t = {};
|
|
37
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
38
|
+
t[p] = s[p];
|
|
39
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
40
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
41
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
42
|
+
t[p[i]] = s[p[i]];
|
|
43
|
+
}
|
|
44
|
+
return t;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
48
|
+
var e = new Error(message);
|
|
49
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
50
|
+
};/********************************************************************************************************************
|
|
51
|
+
* Types
|
|
52
|
+
* ******************************************************************************************************************/
|
|
53
|
+
/********************************************************************************************************************
|
|
54
|
+
* camelCase
|
|
55
|
+
* ******************************************************************************************************************/
|
|
56
|
+
function camelCase(str) {
|
|
57
|
+
const result = str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
|
|
58
|
+
return (result.charAt(0).toUpperCase() + result.slice(1));
|
|
59
|
+
}
|
|
60
|
+
/********************************************************************************************************************
|
|
61
|
+
* makeConst
|
|
62
|
+
* ******************************************************************************************************************/
|
|
63
|
+
function _makeConst(name, items) {
|
|
64
|
+
const aliasValueMap = items.reduce((acc, item) => {
|
|
65
|
+
if (item.length === 3) {
|
|
66
|
+
const [value, , alias] = item;
|
|
67
|
+
acc[alias] = value;
|
|
68
|
+
return acc;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const [value] = item;
|
|
72
|
+
const alias = camelCase(value);
|
|
73
|
+
acc[alias] = value;
|
|
74
|
+
}
|
|
75
|
+
return acc;
|
|
76
|
+
}, {});
|
|
77
|
+
const valueLabelMap = items.reduce((acc, [value, label]) => {
|
|
78
|
+
acc[value] = label;
|
|
79
|
+
return acc;
|
|
80
|
+
}, {});
|
|
81
|
+
const list = items.map((item) => item[0]);
|
|
82
|
+
const nvList = items.map((item) => ({ [name]: item[0], name: item[1] }));
|
|
83
|
+
const lvList = items.map((item) => ({ value: item[0], label: item[1] }));
|
|
84
|
+
return Object.assign(Object.assign({}, aliasValueMap), { getLabel(value) {
|
|
85
|
+
return valueLabelMap[value];
|
|
86
|
+
},
|
|
87
|
+
getList() {
|
|
88
|
+
return [...list];
|
|
89
|
+
},
|
|
90
|
+
getNvList() {
|
|
91
|
+
return nvList.map((item) => (Object.assign({}, item)));
|
|
92
|
+
},
|
|
93
|
+
getLvList(extraPreItems) {
|
|
94
|
+
return [...(extraPreItems || []), ...lvList.map((item) => (Object.assign({}, item)))];
|
|
95
|
+
} });
|
|
96
|
+
}
|
|
97
|
+
function makeConst(nameOrItems, itemsOrUndefined) {
|
|
98
|
+
if (itemsOrUndefined === undefined) {
|
|
99
|
+
const constObj = _makeConst('Unknown', nameOrItems);
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
101
|
+
const { getNvList } = constObj, others = __rest(constObj, ["getNvList"]);
|
|
102
|
+
return Object.assign(Object.assign({}, others), { Type: null });
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const constObj = _makeConst(nameOrItems, itemsOrUndefined);
|
|
106
|
+
return Object.assign(Object.assign({}, constObj), { Type: null });
|
|
107
|
+
}
|
|
18
108
|
}var index = {
|
|
19
109
|
copy,
|
|
20
110
|
lv,
|
|
21
111
|
vl,
|
|
22
|
-
|
|
112
|
+
makeConst,
|
|
113
|
+
};export{copy,index as default,lv,makeConst,vl};
|
package/dist/index.js
CHANGED
|
@@ -15,8 +15,99 @@ function lv(label, value, other) {
|
|
|
15
15
|
* ******************************************************************************************************************/
|
|
16
16
|
function vl(value, label, other) {
|
|
17
17
|
return Object.assign(Object.assign({}, other), { value, label });
|
|
18
|
+
}/******************************************************************************
|
|
19
|
+
Copyright (c) Microsoft Corporation.
|
|
20
|
+
|
|
21
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
22
|
+
purpose with or without fee is hereby granted.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
25
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
26
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
27
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
28
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
29
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
30
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
31
|
+
***************************************************************************** */
|
|
32
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
function __rest(s, e) {
|
|
36
|
+
var t = {};
|
|
37
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
38
|
+
t[p] = s[p];
|
|
39
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
40
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
41
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
42
|
+
t[p[i]] = s[p[i]];
|
|
43
|
+
}
|
|
44
|
+
return t;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
48
|
+
var e = new Error(message);
|
|
49
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
50
|
+
};/********************************************************************************************************************
|
|
51
|
+
* Types
|
|
52
|
+
* ******************************************************************************************************************/
|
|
53
|
+
/********************************************************************************************************************
|
|
54
|
+
* camelCase
|
|
55
|
+
* ******************************************************************************************************************/
|
|
56
|
+
function camelCase(str) {
|
|
57
|
+
const result = str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
|
|
58
|
+
return (result.charAt(0).toUpperCase() + result.slice(1));
|
|
59
|
+
}
|
|
60
|
+
/********************************************************************************************************************
|
|
61
|
+
* makeConst
|
|
62
|
+
* ******************************************************************************************************************/
|
|
63
|
+
function _makeConst(name, items) {
|
|
64
|
+
const aliasValueMap = items.reduce((acc, item) => {
|
|
65
|
+
if (item.length === 3) {
|
|
66
|
+
const [value, , alias] = item;
|
|
67
|
+
acc[alias] = value;
|
|
68
|
+
return acc;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const [value] = item;
|
|
72
|
+
const alias = camelCase(value);
|
|
73
|
+
acc[alias] = value;
|
|
74
|
+
}
|
|
75
|
+
return acc;
|
|
76
|
+
}, {});
|
|
77
|
+
const valueLabelMap = items.reduce((acc, [value, label]) => {
|
|
78
|
+
acc[value] = label;
|
|
79
|
+
return acc;
|
|
80
|
+
}, {});
|
|
81
|
+
const list = items.map((item) => item[0]);
|
|
82
|
+
const nvList = items.map((item) => ({ [name]: item[0], name: item[1] }));
|
|
83
|
+
const lvList = items.map((item) => ({ value: item[0], label: item[1] }));
|
|
84
|
+
return Object.assign(Object.assign({}, aliasValueMap), { getLabel(value) {
|
|
85
|
+
return valueLabelMap[value];
|
|
86
|
+
},
|
|
87
|
+
getList() {
|
|
88
|
+
return [...list];
|
|
89
|
+
},
|
|
90
|
+
getNvList() {
|
|
91
|
+
return nvList.map((item) => (Object.assign({}, item)));
|
|
92
|
+
},
|
|
93
|
+
getLvList(extraPreItems) {
|
|
94
|
+
return [...(extraPreItems || []), ...lvList.map((item) => (Object.assign({}, item)))];
|
|
95
|
+
} });
|
|
96
|
+
}
|
|
97
|
+
function makeConst(nameOrItems, itemsOrUndefined) {
|
|
98
|
+
if (itemsOrUndefined === undefined) {
|
|
99
|
+
const constObj = _makeConst('Unknown', nameOrItems);
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
101
|
+
const { getNvList } = constObj, others = __rest(constObj, ["getNvList"]);
|
|
102
|
+
return Object.assign(Object.assign({}, others), { Type: null });
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const constObj = _makeConst(nameOrItems, itemsOrUndefined);
|
|
106
|
+
return Object.assign(Object.assign({}, constObj), { Type: null });
|
|
107
|
+
}
|
|
18
108
|
}var index = {
|
|
19
109
|
copy,
|
|
20
110
|
lv,
|
|
21
111
|
vl,
|
|
22
|
-
|
|
112
|
+
makeConst,
|
|
113
|
+
};exports.copy=copy;exports.default=index;exports.lv=lv;exports.makeConst=makeConst;exports.vl=vl;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/********************************************************************************************************************
|
|
2
|
+
* Types
|
|
3
|
+
* ******************************************************************************************************************/
|
|
4
|
+
type ValueOf<T> = T[keyof T];
|
|
5
|
+
type IsArray<T> = T extends unknown[] ? true : false;
|
|
6
|
+
/********************************************************************************************************************
|
|
7
|
+
* Types
|
|
8
|
+
* ******************************************************************************************************************/
|
|
9
|
+
type CamelCase<S extends PropertyKey> = S extends string ? S extends `${infer T}_${infer U}` ? `${Capitalize<Lowercase<T>>}${CamelCase<U>}` : Capitalize<Lowercase<S>> : S;
|
|
10
|
+
type TItem = readonly [string, any];
|
|
11
|
+
type TItemWithAlias = readonly [string | number, any, string];
|
|
12
|
+
type TItems = readonly TItem[] | readonly TItemWithAlias[];
|
|
13
|
+
/** MakeAliasMap */
|
|
14
|
+
type MakeAliasValueMap<Items extends TItems> = {
|
|
15
|
+
[K in Items[number] as K extends TItem ? CamelCase<K[0]> : K extends TItemWithAlias ? K[2] : never]: K[0];
|
|
16
|
+
};
|
|
17
|
+
/** MakeValueNameMap */
|
|
18
|
+
type MakeValueLabelMap<Items extends TItems> = {
|
|
19
|
+
[K in Items[number] as K[0]]: K[1];
|
|
20
|
+
};
|
|
21
|
+
/** MakeNameList */
|
|
22
|
+
type MakeNameValueList<Name extends string, Items extends TItems> = Array<ValueOf<{
|
|
23
|
+
[K in Items[number] as K[0]]: {
|
|
24
|
+
[V in Name]: K[0];
|
|
25
|
+
} & {
|
|
26
|
+
label: K[1];
|
|
27
|
+
};
|
|
28
|
+
}>>;
|
|
29
|
+
/** MakeLvList */
|
|
30
|
+
type MakeLabelValueMap<Items extends TItems> = Items extends readonly (infer Item)[] ? Item extends readonly [infer V, infer N, any?] ? {
|
|
31
|
+
value: V;
|
|
32
|
+
label: N;
|
|
33
|
+
} : never : never;
|
|
34
|
+
/********************************************************************************************************************
|
|
35
|
+
* getLvList
|
|
36
|
+
* ******************************************************************************************************************/
|
|
37
|
+
declare function makeConst<StringValue extends string, NumberValue extends number, Label extends string, Alias extends string, const Items extends readonly (readonly [StringValue, Label])[] | readonly (readonly [StringValue | NumberValue, Label, Alias])[], AliasValueMap = MakeAliasValueMap<Items>, ValueLabelMap = MakeValueLabelMap<Items>, ValueList = Items[number][0][], LabelValueMap = MakeLabelValueMap<Items>, ValueType = Items[number][0], GetLabel = <T extends keyof ValueLabelMap>(value: T) => ValueLabelMap[T], GetList = () => ValueList, GetLvList = <LvValue extends string | number, LvLabel extends string, LvItems extends readonly {
|
|
38
|
+
value: LvValue;
|
|
39
|
+
label: LvLabel;
|
|
40
|
+
}[]>(items?: LvItems) => [...ReadonlyArray<LabelValueMap>, ...(IsArray<LvItems> extends true ? LvItems : [])], Result = AliasValueMap & {
|
|
41
|
+
Type: ValueType;
|
|
42
|
+
getLabel: GetLabel;
|
|
43
|
+
getList: GetList;
|
|
44
|
+
getLvList: GetLvList;
|
|
45
|
+
}>(items: Items): Result;
|
|
46
|
+
declare function makeConst<Name extends string, StringValue extends string, NumberValue extends number, Label extends string, Alias extends string, const Items extends readonly (readonly [StringValue, Label])[] | readonly (readonly [StringValue | NumberValue, Label, Alias])[], AliasValueMap = MakeAliasValueMap<Items>, ValueLabelMap = MakeValueLabelMap<Items>, ValueList = Items[number][0][], NameValueList = MakeNameValueList<Name, Items>, LabelValueMap = MakeLabelValueMap<Items>, ValueType = Items[number][0], GetLabel = <T extends keyof ValueLabelMap>(value: T) => ValueLabelMap[T], GetList = () => ValueList, GetNvList = () => NameValueList, GetLvList = <LvValue extends string | number, LvLabel extends string, LvItems extends readonly {
|
|
47
|
+
value: LvValue;
|
|
48
|
+
label: LvLabel;
|
|
49
|
+
}[]>(items?: LvItems) => [...ReadonlyArray<LabelValueMap>, ...(IsArray<LvItems> extends true ? LvItems : [])], Result = AliasValueMap & {
|
|
50
|
+
Type: ValueType;
|
|
51
|
+
getLabel: GetLabel;
|
|
52
|
+
getList: GetList;
|
|
53
|
+
getNvList: GetNvList;
|
|
54
|
+
getLvList: GetLvList;
|
|
55
|
+
}>(name: Name, items: Items): Result;
|
|
56
|
+
export { makeConst };
|
|
57
|
+
export default makeConst;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@pdg/data",
|
|
3
3
|
"title": "Typescript Data Module",
|
|
4
4
|
"description": "Typescript Data Module",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.7",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"module": "./dist/index.esm.js",
|
|
@@ -30,14 +30,15 @@
|
|
|
30
30
|
"scripts": {
|
|
31
31
|
"dev": "cd examples && npm run dev",
|
|
32
32
|
"watchman:del": "watchman watch-del \"${PWD}\" ; watchman watch-project \"${PWD}\"",
|
|
33
|
-
"build": "npm run watchman:del && npm run test &&
|
|
33
|
+
"build": "npm run watchman:del && npm run test && rollup -c --bundleConfigAsCjs",
|
|
34
34
|
"git:commit": "node .git-commit.cjs",
|
|
35
35
|
"git:push": "git push",
|
|
36
36
|
"git:commit:push": "npm run git:commit && npm run git:push",
|
|
37
37
|
"git:merge:mirror": "node .git-merge.cjs mirror main",
|
|
38
38
|
"reset:gitignore": "git rm -r --cached . && git add .",
|
|
39
|
-
"pub": "npm i && npm run build && npm publish --access=public && rm ./.git/hooks/pre-commit",
|
|
39
|
+
"pub": "npm i && npm run build && npm publish --access=public && [ -f ./.git/hooks/pre-commit ] && rm ./.git/hooks/pre-commit || true",
|
|
40
40
|
"lint": "eslint './src/**/*.ts'",
|
|
41
|
+
"tsc": "tsc --noEmit",
|
|
41
42
|
"reinstall": "npm run reinstall:module",
|
|
42
43
|
"reinstall:module": "rm -rf node_modules && rm -f package-lock.json && npm i",
|
|
43
44
|
"test": "jest"
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@eslint/js": "9.39.1",
|
|
55
56
|
"@rollup/plugin-commonjs": "29.0.0",
|
|
57
|
+
"@rollup/plugin-eslint": "^9.2.0",
|
|
56
58
|
"@rollup/plugin-node-resolve": "16.0.3",
|
|
57
59
|
"@types/jest": "29.5.14",
|
|
58
60
|
"@types/node": "22.19.2",
|