This commit is contained in:
zhujingjing 2024-01-29 00:10:57 +08:00
parent d6e6a58a36
commit d14fac052c
Signed by: karlcw
GPG Key ID: B11805D3A0F5C671
7 changed files with 193 additions and 72 deletions

3
lib/index.d.ts vendored
View File

@ -1,4 +1,5 @@
import { withRecord, assignRecords, RecordClearMode } from "./commons/record-util"; import { withRecord, assignRecords, RecordClearMode } from "./commons/record-util";
import { stringTokenizer, equalsIgnoreCase, includeIgnoreCase, strLength, strMonospacePad } from "./commons/str-utils"; import { stringTokenizer, equalsIgnoreCase, includeIgnoreCase, strLength, strMonospacePad } from "./commons/str-utils";
import { getFileExt, getFileFullName, getFileNameWithoutExt, getFileInfo } from "./commons/file-util"; import { getFileExt, getFileFullName, getFileNameWithoutExt, getFileInfo } from "./commons/file-util";
export { stringTokenizer, withRecord, equalsIgnoreCase, includeIgnoreCase, assignRecords, RecordClearMode, getFileExt, getFileFullName as getFileName, getFileNameWithoutExt, getFileInfo, strLength, strMonospacePad }; import { Optional } from "./commons/optional";
export { Optional, RecordClearMode, assignRecords, equalsIgnoreCase, getFileExt, getFileFullName as getFileName, getFileInfo, getFileNameWithoutExt, includeIgnoreCase, strLength, strMonospacePad, stringTokenizer, withRecord, };

View File

@ -1,5 +1,5 @@
var a = /* @__PURE__ */ ((e) => (e[e.delete = 2] = "delete", e[e.reset = 1] = "reset", e))(a || {}); var h = /* @__PURE__ */ ((e) => (e[e.delete = 2] = "delete", e[e.reset = 1] = "reset", e))(h || {});
const d = (e) => ({ const m = (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;
@ -14,51 +14,51 @@ const d = (e) => ({
), Object.assign(e, t); ), Object.assign(e, t);
} }
}); });
function m(...e) { function p(...e) {
let t = e ?? []; let t = e ?? [];
return { return {
test: function(n) { test: function(n) {
if (t.length <= 1) if (t.length <= 1)
return t.length === 1 ? t[0] : []; return t.length === 1 ? t[0] : [];
let i = n ?? function(s, r) { let s = n ?? function(r, i) {
return s === r; return r === i;
}; };
return t.reduce((s, r) => (r && r.length > 0 && (s.length === 0 ? s.push(...r) : r.forEach((u) => { return t.reduce((r, i) => (i && i.length > 0 && (r.length === 0 ? r.push(...i) : i.forEach((f) => {
s.some((l) => i(l, u)) || s.push(u); r.some((u) => s(u, f)) || r.push(f);
})), s), []); })), r), []);
}, },
path: function(...n) { path: function(...n) {
return this.test((i, s) => n.every((r) => i[r] === s[r])); return this.test((s, r) => n.every((i) => s[i] === r[i]));
} }
}; };
} }
function I(e, t, n) { function I(e, t, n) {
const i = t, s = n || t; const s = t, r = n || t;
return function(r) { return function(i) {
const u = [], f = r.matchAll(e); const f = [], l = i.matchAll(e);
let l = 0; let u = 0;
for (const o of f) { for (const o of l) {
const c = r.slice(l, o.index); const g = i.slice(u, o.index);
c && u.push(s(c, !1)), u.push(i(o[0], !0)), l = o.index + o[0].length; g && f.push(r(g, !1)), f.push(s(o[0], !0)), u = o.index + o[0].length;
} }
return l < r.length && u.push(s(r.slice(l), !1)), u; return u < i.length && f.push(r(i.slice(u), !1)), f;
}; };
} }
function g(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 O = (e, t) => { const O = (e, t) => {
for (let n = 0; n < e.length; n++) { for (let n = 0; n < e.length; n++) {
let i = e[n]; let s = e[n];
if (g(i, t)) if (a(s, t))
return !0; return !0;
} }
return !1; return !1;
}, h = (e) => e.replaceAll(/[\u4e00-\u9fa5]/g, "--").length, p = (e, t, n = " ") => { }, d = (e) => e.replaceAll(/[\u4e00-\u9fa5]/g, "--").length, E = (e, t, n = " ") => {
const i = t - (h(e) - e.length); const s = t - (d(e) - e.length);
return e.padEnd(i, n); return e.padEnd(s, n);
}; };
function E(e) { function b(e) {
const t = e.lastIndexOf("."); const t = e.lastIndexOf(".");
return t === -1 ? "" : e.substring(t + 1); return t === -1 ? "" : e.substring(t + 1);
} }
@ -66,36 +66,74 @@ function x(e) {
let t = e.lastIndexOf("/"); let t = e.lastIndexOf("/");
return t === -1 && (t = e.lastIndexOf("\\"), t === -1) ? e : e.substring(t + 1); return t === -1 && (t = e.lastIndexOf("\\"), t === -1) ? e : e.substring(t + 1);
} }
function b(e) { function y(e) {
const t = x(e), n = t.lastIndexOf("."); const t = x(e), n = t.lastIndexOf(".");
return n === -1 ? t : t.substring(0, n); return n === -1 ? t : t.substring(0, n);
} }
function y(e, t = "win32") { function N(e, t = "win32") {
const n = t === "win32" ? "\\" : "/"; const n = t === "win32" ? "\\" : "/";
let i, s, r, u; let s, r, i, f;
const f = e.lastIndexOf(n); const l = e.lastIndexOf(n);
f === -1 ? (i = "", s = e) : (i = e.substring(0, f + 1), s = e.substring(f + 1)); l === -1 ? (s = "", r = e) : (s = e.substring(0, l + 1), r = e.substring(l + 1));
const l = s.lastIndexOf("."); const u = r.lastIndexOf(".");
return l === -1 ? (r = "", u = s) : (r = s.substring(l + 1), u = s.substring(0, l)), { return u === -1 ? (i = "", f = r) : (i = r.substring(u + 1), f = r.substring(0, u)), {
fileName: s, fileName: r,
fileNameNoExt: u, fileNameNoExt: f,
fileExtName: r, fileExtName: i,
fullPath: e, fullPath: e,
pathOnly: i pathOnly: s
}; };
} }
const c = {
/**
* Creates an optional of the specified value. The value may be undefined or null.
* @return OptionalInstance<T>
*/
of(e) {
function t(n) {
return n == null;
}
return {
get() {
return e;
},
orElse(n) {
return t(e) ? n : e;
},
then(n, s) {
t(e) ? s && s() : n(e);
},
ifPresent(n) {
e != null && n(e);
},
isPresent() {
return !t(e);
},
filter(n) {
return t(e) || n(e) ? this : c.of(void 0);
},
map(n) {
return e == null ? c.of(void 0) : c.of(n(e));
},
flatMap(n) {
return n(e);
}
};
}
};
export { export {
a as RecordClearMode, c as Optional,
m as assignRecords, h as RecordClearMode,
g as equalsIgnoreCase, p as assignRecords,
E as getFileExt, a as equalsIgnoreCase,
y as getFileInfo, b as getFileExt,
N as getFileInfo,
x as getFileName, x as getFileName,
b as getFileNameWithoutExt, y as getFileNameWithoutExt,
O as includeIgnoreCase, O as includeIgnoreCase,
h as strLength, d as strLength,
p as strMonospacePad, E as strMonospacePad,
I as stringTokenizer, I as stringTokenizer,
d as withRecord m 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(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 I=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 x(...e){let n=e??[];return{test:function(t){if(n.length<=1)return n.length===1?n[0]:[];let u=t??function(i,s){return i===s};return n.reduce((i,s)=>(s&&s.length>0&&(i.length===0?i.push(...s):s.forEach(l=>{i.some(f=>u(f,l))||i.push(l)})),i),[])},path:function(...t){return this.test((u,i)=>t.every(s=>u[s]===i[s]))}}}function b(e,n,t){const u=n,i=t||n;return function(s){const l=[],c=s.matchAll(e);let f=0;for(const a of c){const m=s.slice(f,a.index);m&&l.push(i(m,!1)),l.push(u(a[0],!0)),f=a.index+a[0].length}return f<s.length&&l.push(i(s.slice(f),!1)),l}}function g(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(g(u,n))return!0}return!1},d=e=>e.replaceAll(/[\u4e00-\u9fa5]/g,"--").length,E=(e,n,t=" ")=>{const u=n-(d(e)-e.length);return e.padEnd(u,t)};function O(e){const n=e.lastIndexOf(".");return n===-1?"":e.substring(n+1)}function h(e){let n=e.lastIndexOf("/");return n===-1&&(n=e.lastIndexOf("\\"),n===-1)?e:e.substring(n+1)}function F(e){const n=h(e),t=n.lastIndexOf(".");return t===-1?n:n.substring(0,t)}function N(e,n="win32"){const t=n==="win32"?"\\":"/";let u,i,s,l;const c=e.lastIndexOf(t);c===-1?(u="",i=e):(u=e.substring(0,c+1),i=e.substring(c+1));const f=i.lastIndexOf(".");return f===-1?(s="",l=i):(s=i.substring(f+1),l=i.substring(0,f)),{fileName:i,fileNameNoExt:l,fileExtName:s,fullPath:e,pathOnly:u}}r.RecordClearMode=o,r.assignRecords=x,r.equalsIgnoreCase=g,r.getFileExt=O,r.getFileInfo=N,r.getFileName=h,r.getFileNameWithoutExt=F,r.includeIgnoreCase=y,r.strLength=d,r.strMonospacePad=E,r.stringTokenizer=b,r.withRecord=I,Object.defineProperty(r,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 O=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 E(...e){let n=e??[];return{test:function(t){if(n.length<=1)return n.length===1?n[0]:[];let f=t??function(i,s){return i===s};return n.reduce((i,s)=>(s&&s.length>0&&(i.length===0?i.push(...s):s.forEach(u=>{i.some(l=>f(l,u))||i.push(u)})),i),[])},path:function(...t){return this.test((f,i)=>t.every(s=>f[s]===i[s]))}}}function b(e,n,t){const f=n,i=t||n;return function(s){const u=[],c=s.matchAll(e);let l=0;for(const d of c){const I=s.slice(l,d.index);I&&u.push(i(I,!1)),u.push(f(d[0],!0)),l=d.index+d[0].length}return l<s.length&&u.push(i(s.slice(l),!1)),u}}function a(e,n){return typeof e=="string"&&typeof n=="string"?e.toLowerCase()===n.toLowerCase():e===void 0&&n===void 0}const x=(e,n)=>{for(let t=0;t<e.length;t++){let f=e[t];if(a(f,n))return!0}return!1},h=e=>e.replaceAll(/[\u4e00-\u9fa5]/g,"--").length,y=(e,n,t=" ")=>{const f=n-(h(e)-e.length);return e.padEnd(f,t)};function p(e){const n=e.lastIndexOf(".");return n===-1?"":e.substring(n+1)}function m(e){let n=e.lastIndexOf("/");return n===-1&&(n=e.lastIndexOf("\\"),n===-1)?e:e.substring(n+1)}function F(e){const n=m(e),t=n.lastIndexOf(".");return t===-1?n:n.substring(0,t)}function N(e,n="win32"){const t=n==="win32"?"\\":"/";let f,i,s,u;const c=e.lastIndexOf(t);c===-1?(f="",i=e):(f=e.substring(0,c+1),i=e.substring(c+1));const l=i.lastIndexOf(".");return l===-1?(s="",u=i):(s=i.substring(l+1),u=i.substring(0,l)),{fileName:i,fileNameNoExt:u,fileExtName:s,fullPath:e,pathOnly:f}}const g={of(e){function n(t){return t==null}return{get(){return e},orElse(t){return n(e)?t:e},then(t,f){n(e)?f&&f():t(e)},ifPresent(t){e!=null&&t(e)},isPresent(){return!n(e)},filter(t){return n(e)||t(e)?this:g.of(void 0)},map(t){return e==null?g.of(void 0):g.of(t(e))},flatMap(t){return t(e)}}}};r.Optional=g,r.RecordClearMode=o,r.assignRecords=E,r.equalsIgnoreCase=a,r.getFileExt=p,r.getFileInfo=N,r.getFileName=m,r.getFileNameWithoutExt=F,r.includeIgnoreCase=x,r.strLength=h,r.strMonospacePad=y,r.stringTokenizer=b,r.withRecord=O,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

80
src/commons/optional.ts Normal file
View File

@ -0,0 +1,80 @@
/*
* Copyright 2015,2016 DevCon5 GmbH, info@devcon5.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* Optional monad for Javascript | Typescript.
*/
export const Optional = {
/**
* Creates an optional of the specified value. The value may be undefined or null.
* @return OptionalInstance<T>
*/
of<T>(value: T) {
function notPresent(value: any): value is null {
return value == null;
}
return {
get() {
return value;
},
orElse(elseValue: NonNullable<T>): NonNullable<T> {
if (notPresent(value)) {
return elseValue;
}
return value as NonNullable<T>;
},
then(
presentFunction: (value: T) => void,
notPresentFunction?: () => void
) {
if (!notPresent(value)) {
presentFunction(value);
} else if (notPresentFunction) {
notPresentFunction();
}
},
ifPresent<R>(func: (val: NonNullable<T>) => R) {
if (value != null) {
func(value);
}
},
isPresent() {
return !notPresent(value);
},
filter(filterFunction: (value: T) => boolean) {
if (notPresent(value) || filterFunction(value)) {
return this;
} else {
return Optional.of(undefined);
}
},
map<R>(mapperFunction: (value: NonNullable<T>) => R) {
if (value == null) {
return Optional.of(undefined);
} else {
return Optional.of(mapperFunction(value));
}
},
flatMap<R>(mapperFunction: (value: T) => R) {
return mapperFunction(value);
},
};
},
};
export default Optional;

View File

@ -8,7 +8,7 @@ import {
equalsIgnoreCase, equalsIgnoreCase,
includeIgnoreCase, includeIgnoreCase,
strLength, strLength,
strMonospacePad strMonospacePad,
} from "./commons/str-utils"; } from "./commons/str-utils";
import { import {
getFileExt, getFileExt,
@ -16,18 +16,20 @@ import {
getFileNameWithoutExt, getFileNameWithoutExt,
getFileInfo, getFileInfo,
} from "./commons/file-util"; } from "./commons/file-util";
import { Optional } from "./commons/optional";
export { export {
stringTokenizer, Optional,
withRecord,
equalsIgnoreCase,
includeIgnoreCase,
assignRecords,
RecordClearMode, RecordClearMode,
assignRecords,
equalsIgnoreCase,
getFileExt, getFileExt,
getFileFullName as getFileName, getFileFullName as getFileName,
getFileNameWithoutExt,
getFileInfo, getFileInfo,
getFileNameWithoutExt,
includeIgnoreCase,
strLength, strLength,
strMonospacePad strMonospacePad,
stringTokenizer,
withRecord,
}; };