new functions

This commit is contained in:
zhujingjing 2023-11-11 21:28:01 +08:00
parent ab9bcd61e1
commit 698f4f22e4
Signed by: karlcw
GPG Key ID: B11805D3A0F5C671
10 changed files with 192 additions and 60 deletions

View File

@ -1,11 +1,33 @@
import { UnwrapNestedRefs } from 'vue'; import { UnwrapNestedRefs } from "vue";
declare enum ClearMode { export declare enum RecordClearMode {
clear = 3,
delete = 2, delete = 2,
reset = 1 reset = 1
} }
export declare const withRecord: (obj: UnwrapNestedRefs<Record<string, any>>) => { export declare const withRecord: (obj: UnwrapNestedRefs<Record<string, any>>) => {
clear: (mode?: ClearMode) => void; clear: (mode?: RecordClearMode) => void;
replace: (newVal: UnwrapNestedRefs<Record<string, any>>) => void; replace: (newVal: UnwrapNestedRefs<Record<string, any>>) => void;
}; };
export {}; /**
*
* test path
* test
* path
* @param arrays
* @returns
*/
export declare function assignRecords<T extends Record<string, any>>(...arrays: T[][]): {
test: (equal?: ((l: T, r: T) => boolean) | undefined) => T[];
path: (...paths: string[]) => T[];
};
/**
*
*
*
*
*
* @param target
* @param sources
* @returns
*/
export declare const extend: <T extends Record<string, any>>(target: T, ...sources: (T | undefined)[]) => T;

4
lib/index.d.ts vendored
View File

@ -1,3 +1,3 @@
import { withRecord } from "./commons/record-util"; import { withRecord, assignRecords, RecordClearMode } from "./commons/record-util";
import { stringTokenizer, equalsIgnoreCase, includeIgnoreCase } from "./commons/str-utils"; import { stringTokenizer, equalsIgnoreCase, includeIgnoreCase } from "./commons/str-utils";
export { stringTokenizer, withRecord, equalsIgnoreCase, includeIgnoreCase }; export { stringTokenizer, withRecord, equalsIgnoreCase, includeIgnoreCase, assignRecords, RecordClearMode };

View File

@ -1,45 +1,66 @@
const h = (e) => ({ var c = /* @__PURE__ */ ((e) => (e[e.delete = 2] = "delete", e[e.reset = 1] = "reset", e))(c || {});
const g = (e) => ({
clear: function(t = 2) { clear: function(t = 2) {
t === 1 ? Object.keys(e).forEach((n) => { t === 1 ? Object.keys(e).forEach((n) => {
e[n] = void 0; e[n] = void 0;
}) : t === 2 ? Object.keys(e).forEach((n) => { }) : t === 2 && Object.keys(e).forEach((n) => {
delete e[n]; delete e[n];
}) : Object.assign(e, {}); });
}, },
replace: function(t) { replace: function(t) {
this.clear( this.clear(
3 2
/* clear */ /* delete */
), Object.assign(e, t); ), Object.assign(e, t);
} }
}); });
function g(e, t, n) { function p(...e) {
const f = t, o = n || t; let t = e ?? [];
return function(i) { return {
const s = [], u = i.matchAll(e); test: function(n) {
let r = 0; if (t.length <= 1)
for (const c of u) { return t.length === 1 ? t[0] : [];
const l = i.slice(r, c.index); let i = n ?? function(s, r) {
l && s.push(o(l, !1)), s.push(f(c[0], !0)), r = c.index + c[0].length; return s === r;
};
return t.reduce((s, r) => (r && r.length > 0 && (s.length === 0 ? s.push(...r) : r.forEach((u) => {
s.some((f) => i(f, u)) || s.push(u);
})), s), []);
},
path: function(...n) {
return this.test((i, s) => n.every((r) => i[r] === s[r]));
} }
return r < i.length && s.push(o(i.slice(r), !1)), s; };
}
function y(e, t, n) {
const i = t, s = n || t;
return function(r) {
const u = [], l = r.matchAll(e);
let f = 0;
for (const o of l) {
const h = r.slice(f, o.index);
h && u.push(s(h, !1)), u.push(i(o[0], !0)), f = o.index + o[0].length;
}
return f < r.length && u.push(s(r.slice(f), !1)), u;
}; };
} }
function a(e, t) { function a(e, t) {
return typeof e == "string" && typeof t == "string" ? e.toLowerCase() === t.toLowerCase() : e === void 0 && t === void 0; return typeof e == "string" && typeof t == "string" ? e.toLowerCase() === t.toLowerCase() : e === void 0 && t === void 0;
} }
const p = (e, t) => { const m = (e, t) => {
for (let n = 0; n < e.length; n++) { for (let n = 0; n < e.length; n++) {
let f = e[n]; let i = e[n];
if (a(f, t)) if (a(i, t))
return !0; return !0;
} }
return !1; return !1;
}; };
export { export {
c as RecordClearMode,
p as assignRecords,
a as equalsIgnoreCase, a as equalsIgnoreCase,
p as includeIgnoreCase, m as includeIgnoreCase,
g as stringTokenizer, y as stringTokenizer,
h as withRecord g as withRecord
}; };
//# sourceMappingURL=index.es.js.map //# sourceMappingURL=index.es.js.map

File diff suppressed because one or more lines are too long

View File

@ -1,2 +1,2 @@
(function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i["ez-common-ts"]={}))})(this,function(i){"use strict";const s=e=>({clear:function(n=2){n===1?Object.keys(e).forEach(t=>{e[t]=void 0}):n===2?Object.keys(e).forEach(t=>{delete e[t]}):Object.assign(e,{})},replace:function(n){this.clear(3),Object.assign(e,n)}});function h(e,n,t){const u=n,a=t||n;return function(o){const f=[],p=o.matchAll(e);let c=0;for(const r of p){const d=o.slice(c,r.index);d&&f.push(a(d,!1)),f.push(u(r[0],!0)),c=r.index+r[0].length}return c<o.length&&f.push(a(o.slice(c),!1)),f}}function l(e,n){return typeof e=="string"&&typeof n=="string"?e.toLowerCase()===n.toLowerCase():e===void 0&&n===void 0}const g=(e,n)=>{for(let t=0;t<e.length;t++){let u=e[t];if(l(u,n))return!0}return!1};i.equalsIgnoreCase=l,i.includeIgnoreCase=g,i.stringTokenizer=h,i.withRecord=s,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}); (function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r["ez-common-ts"]={}))})(this,function(r){"use strict";var o=(e=>(e[e.delete=2]="delete",e[e.reset=1]="reset",e))(o||{});const g=e=>({clear:function(n=2){n===1?Object.keys(e).forEach(t=>{e[t]=void 0}):n===2&&Object.keys(e).forEach(t=>{delete e[t]})},replace:function(n){this.clear(2),Object.assign(e,n)}});function m(...e){let n=e??[];return{test:function(t){if(n.length<=1)return n.length===1?n[0]:[];let u=t??function(s,i){return s===i};return n.reduce((s,i)=>(i&&i.length>0&&(s.length===0?s.push(...i):i.forEach(f=>{s.some(l=>u(l,f))||s.push(f)})),s),[])},path:function(...t){return this.test((u,s)=>t.every(i=>u[i]===s[i]))}}}function p(e,n,t){const u=n,s=t||n;return function(i){const f=[],a=i.matchAll(e);let l=0;for(const c of a){const d=i.slice(l,c.index);d&&f.push(s(d,!1)),f.push(u(c[0],!0)),l=c.index+c[0].length}return l<i.length&&f.push(s(i.slice(l),!1)),f}}function h(e,n){return typeof e=="string"&&typeof n=="string"?e.toLowerCase()===n.toLowerCase():e===void 0&&n===void 0}const y=(e,n)=>{for(let t=0;t<e.length;t++){let u=e[t];if(h(u,n))return!0}return!1};r.RecordClearMode=o,r.assignRecords=m,r.equalsIgnoreCase=h,r.includeIgnoreCase=y,r.stringTokenizer=p,r.withRecord=g,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=index.umd.js.map //# sourceMappingURL=index.umd.js.map

File diff suppressed because one or more lines are too long

View File

@ -12,6 +12,7 @@
"test": "vitest" "test": "vitest"
}, },
"devDependencies": { "devDependencies": {
"prettier": "^3.0.3",
"typescript": "^5.0.2", "typescript": "^5.0.2",
"vite": "^4.4.5", "vite": "^4.4.5",
"vite-plugin-dts": "^3.6.3", "vite-plugin-dts": "^3.6.3",
@ -20,7 +21,8 @@
"exports": { "exports": {
".": { ".": {
"import": "./lib/index.es.js", "import": "./lib/index.es.js",
"require": "./lib/index.umd.js" "require": "./lib/index.umd.js",
"types": "./lib/index.d.ts"
} }
}, },
"dependencies": { "dependencies": {

View File

@ -10,6 +10,9 @@ dependencies:
version: registry.npmmirror.com/vitest@0.34.6 version: registry.npmmirror.com/vitest@0.34.6
devDependencies: devDependencies:
prettier:
specifier: ^3.0.3
version: registry.npmmirror.com/prettier@3.0.3
typescript: typescript:
specifier: ^5.0.2 specifier: ^5.0.2
version: registry.npmmirror.com/typescript@5.2.2 version: registry.npmmirror.com/typescript@5.2.2
@ -1095,6 +1098,14 @@ packages:
picocolors: registry.npmmirror.com/picocolors@1.0.0 picocolors: registry.npmmirror.com/picocolors@1.0.0
source-map-js: registry.npmmirror.com/source-map-js@1.0.2 source-map-js: registry.npmmirror.com/source-map-js@1.0.2
registry.npmmirror.com/prettier@3.0.3:
resolution: {integrity: sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/prettier/-/prettier-3.0.3.tgz}
name: prettier
version: 3.0.3
engines: {node: '>=14'}
hasBin: true
dev: true
registry.npmmirror.com/pretty-format@29.7.0: registry.npmmirror.com/pretty-format@29.7.0:
resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/pretty-format/-/pretty-format-29.7.0.tgz} resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/pretty-format/-/pretty-format-29.7.0.tgz}
name: pretty-format name: pretty-format

View File

@ -1,30 +1,106 @@
import { UnwrapNestedRefs } from 'vue'; import { UnwrapNestedRefs } from "vue";
enum ClearMode { export enum RecordClearMode {
clear = 3,
delete = 2, delete = 2,
reset = 1 reset = 1,
} }
export const withRecord = ( export const withRecord = (obj: UnwrapNestedRefs<Record<string, any>>) => {
obj: UnwrapNestedRefs<Record<string, any>>,
) => {
return { return {
clear: function (mode: ClearMode = ClearMode.delete) { clear: function (mode: RecordClearMode = RecordClearMode.delete) {
if (mode === ClearMode.reset) { if (mode === RecordClearMode.reset) {
Object.keys(obj).forEach((key) => { Object.keys(obj).forEach((key) => {
obj[key] = undefined; obj[key] = undefined;
}); });
} else if (mode === ClearMode.delete) { } else if (mode === RecordClearMode.delete) {
Object.keys(obj).forEach((key) => { Object.keys(obj).forEach((key) => {
delete obj[key]; delete obj[key];
}); });
} else {
Object.assign(obj, {});
} }
}, },
replace: function (newVal: UnwrapNestedRefs<Record<string, any>>) { replace: function (newVal: UnwrapNestedRefs<Record<string, any>>) {
this.clear(ClearMode.clear); this.clear(RecordClearMode.delete);
Object.assign(obj, newVal); Object.assign(obj, newVal);
},
};
};
/**
*
* test path
* test
* path
* @param arrays
* @returns
*/
export function assignRecords<T extends Record<string, any>>(...arrays: T[][]) {
let _arrays = arrays ?? [];
return {
test: function (equal?: (l: T, r: T) => boolean) {
if (_arrays.length <= 1) {
return _arrays.length === 1 ? _arrays[0] : [];
} }
let localEqual =
equal ??
function (ll: T, rr: T) {
return ll === rr;
}; };
return _arrays.reduce((acc, crt) => {
if (crt && crt.length > 0) {
if (acc.length === 0) {
acc.push(...crt);
} else {
crt.forEach((c) => {
const some = acc.some((a) => localEqual(a, c));
if (!some) {
acc.push(c);
}
});
}
}
return acc;
}, []);
},
path: function (...paths: string[]) {
return this.test((l, r) => {
return paths.every((path) => {
return l[path] === r[path];
});
});
},
};
}
/**
*
*
*
*
*
* @param target
* @param sources
* @returns
*/
export const extend = <T extends Record<string, any>>(
// 目标对象
target: T,
// 其他对象参数
...sources: (T | undefined)[]
): T => {
// 遍历其他对象参数
for (const source of sources) {
if (source !== undefined) {
// 遍历参数对象的属性
for (const key in source) {
// 判断属性存在于参数对象中且值不为 null
if (source.hasOwnProperty(key) && source[key] != null) {
// 将属性赋值给目标对象
target[key] = source[key];
}
}
}
}
// 返回目标对象
return target;
}; };

View File

@ -1,4 +1,4 @@
import { withRecord } from "./commons/record-util"; import { withRecord, assignRecords, RecordClearMode } from "./commons/record-util";
import { stringTokenizer, equalsIgnoreCase, includeIgnoreCase } from "./commons/str-utils"; import { stringTokenizer, equalsIgnoreCase, includeIgnoreCase } from "./commons/str-utils";
export { stringTokenizer, withRecord, equalsIgnoreCase, includeIgnoreCase }; export { stringTokenizer, withRecord, equalsIgnoreCase, includeIgnoreCase, assignRecords, RecordClearMode };