@lxlib/util
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.
1,278 lines (1,261 loc) • 37.7 kB
JavaScript
import extend from 'extend';
import addDays from 'date-fns/addDays';
import endOfDay from 'date-fns/endOfDay';
import endOfMonth from 'date-fns/endOfMonth';
import endOfWeek from 'date-fns/endOfWeek';
import endOfYear from 'date-fns/endOfYear';
import parseISO from 'date-fns/parseISO';
import startOfDay from 'date-fns/startOfDay';
import startOfMonth from 'date-fns/startOfMonth';
import startOfWeek from 'date-fns/startOfWeek';
import startOfYear from 'date-fns/startOfYear';
import subMonths from 'date-fns/subMonths';
import subWeeks from 'date-fns/subWeeks';
import subYears from 'date-fns/subYears';
import { DOCUMENT, CommonModule } from '@angular/common';
import { Injectable, Inject, ɵɵdefineInjectable, ɵɵinject, isDevMode, NgModule } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { share, filter } from 'rxjs/operators';
import { environment } from 'ng-zorro-antd/core/environments';
import { NzTreeNode } from 'ng-zorro-antd/core/tree';
/**
* @fileoverview added by tsickle
* Generated from: src/other/other.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* 类似 `_.get`,根据 `path` 获取安全值
* jsperf: https://jsperf.com/es-deep-getttps://jsperf.com/es-deep-get
*
* @param {?} obj 数据源,无效时直接返回 `defaultValue` 值
* @param {?} path 若 `null`、`[]`、未定义及未找到时返回 `defaultValue` 值
* @param {?=} defaultValue 默认值
* @return {?}
*/
function deepGet(obj, path, defaultValue) {
if (!obj || path == null || path.length === 0)
return defaultValue;
if (!Array.isArray(path)) {
path = ~path.indexOf('.') ? path.split('.') : [path];
}
if (path.length === 1) {
/** @type {?} */
const checkObj = obj[path[0]];
return typeof checkObj === 'undefined' ? defaultValue : checkObj;
}
/** @type {?} */
const res = path.reduce((/**
* @param {?} o
* @param {?} k
* @return {?}
*/
(o, k) => (o || {})[k]), obj);
return typeof res === 'undefined' ? defaultValue : res;
}
/**
* 基于 [extend](https://github.com/justmoon/node-extend) 的深度拷贝
* @param {?} obj
* @return {?}
*/
function deepCopy(obj) {
/** @type {?} */
const result = extend(true, {}, { _: obj });
return result._;
}
/**
* 复制字符串文档至剪贴板
* @param {?} value
* @return {?}
*/
function copy(value) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
(resolve) => {
/** @type {?} */
let copyTextArea = null;
try {
copyTextArea = document.createElement('textarea');
copyTextArea.style.height = '0px';
copyTextArea.style.opacity = '0';
copyTextArea.style.width = '0px';
document.body.appendChild(copyTextArea);
copyTextArea.value = value;
copyTextArea.select();
document.execCommand('copy');
resolve(value);
}
finally {
if (copyTextArea && copyTextArea.parentNode) {
copyTextArea.parentNode.removeChild(copyTextArea);
}
}
}));
}
/**
* 深度合并对象
*
* @param {?} original 原始对象
* @param {?} ingoreArray 是否忽略数组,`true` 表示忽略数组的合并,`false` 表示会合并整个数组
* @param {...?} objects 要合并的对象
* @return {?}
*/
function deepMergeKey(original, ingoreArray, ...objects) {
if (Array.isArray(original) || typeof original !== 'object')
return original;
/** @type {?} */
const isObject = (/**
* @param {?} v
* @return {?}
*/
(v) => typeof v === 'object' || typeof v === 'function');
/** @type {?} */
const merge = (/**
* @param {?} target
* @param {?} obj
* @return {?}
*/
(target, obj) => {
Object.keys(obj)
.filter((/**
* @param {?} key
* @return {?}
*/
key => key !== '__proto__' && Object.prototype.hasOwnProperty.call(obj, key)))
.forEach((/**
* @param {?} key
* @return {?}
*/
key => {
/** @type {?} */
const oldValue = obj[key];
/** @type {?} */
const newValue = target[key];
if (Array.isArray(newValue)) {
target[key] = ingoreArray ? oldValue : [...newValue, ...oldValue];
}
else if (oldValue != null && isObject(oldValue) && newValue != null && isObject(newValue)) {
target[key] = merge(newValue, oldValue);
}
else {
target[key] = deepCopy(oldValue);
}
}));
return target;
});
objects.filter((/**
* @param {?} v
* @return {?}
*/
v => v != null && isObject(v))).forEach((/**
* @param {?} v
* @return {?}
*/
v => merge(original, v)));
return original;
}
/**
* 深度合并对象
*
* @param {?} original 原始对象
* @param {...?} objects 要合并的对象
* @return {?}
*/
function deepMerge(original, ...objects) {
return deepMergeKey(original, false, ...objects);
}
/**
* @fileoverview added by tsickle
* Generated from: src/string/string.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* 字符串格式化
* ```
* format('this is ${name}', { name: 'asdf' })
* // output: this is asdf
* format('this is ${user.name}', { user: { name: 'asdf' } }, true)
* // output: this is asdf
* ```
* @param {?} str
* @param {?} obj
* @param {?=} needDeepGet
* @return {?}
*/
function format(str, obj, needDeepGet = false) {
return (str || '').replace(/\${([^}]+)}/g, (/**
* @param {?} _work
* @param {?} key
* @return {?}
*/
(_work, key) => needDeepGet ? deepGet(obj, key.split('.'), '') : (obj || {})[key] || ''));
}
/**
* @fileoverview added by tsickle
* Generated from: src/time/time.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* 获取时间范围
* @param {?} type 类型,带 `-` 表示过去一个时间,若指定 `number` 表示天数
* @param {?=} time 开始时间
* @return {?}
*/
function getTimeDistance(type, time) {
time = time ? (typeof time === 'string' ? parseISO(time) : new Date(time)) : new Date();
/** @type {?} */
const options = { weekStartsOn: 1 };
/** @type {?} */
let res;
switch (type) {
case 'today':
res = [time, time];
break;
case '-today':
res = [addDays(time, -1), time];
break;
case 'yesterday':
res = [addDays(time, -1), addDays(time, -1)];
break;
case 'week':
res = [startOfWeek(time, options), endOfWeek(time, options)];
break;
case '-week':
res = [startOfWeek(subWeeks(time, 1), options), endOfWeek(subWeeks(time, 1), options)];
break;
case 'month':
res = [startOfMonth(time), endOfMonth(time)];
break;
case '-month':
res = [startOfMonth(subMonths(time, 1)), endOfMonth(subMonths(time, 1))];
break;
case 'year':
res = [startOfYear(time), endOfYear(time)];
break;
case '-year':
res = [startOfYear(subYears(time, 1)), endOfYear(subYears(time, 1))];
break;
default:
res = type > 0 ? [time, addDays(time, type)] : [addDays(time, type), time];
break;
}
return fixEndTimeOfRange(res);
}
/**
* fix time is the most, big value
* @param {?} dates
* @return {?}
*/
function fixEndTimeOfRange(dates) {
return [startOfDay(dates[0]), endOfDay(dates[1])];
}
/**
* @fileoverview added by tsickle
* Generated from: src/lazy/lazy.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function LazyResult() { }
if (false) {
/** @type {?} */
LazyResult.prototype.path;
/** @type {?} */
LazyResult.prototype.loaded;
/** @type {?} */
LazyResult.prototype.status;
/** @type {?|undefined} */
LazyResult.prototype.error;
}
/**
* 延迟加载资源(js 或 css)服务
*/
class LazyService {
/**
* @param {?} doc
*/
constructor(doc) {
this.doc = doc;
this.list = {};
this.cached = {};
this._notify = new BehaviorSubject([]);
}
/**
* @return {?}
*/
get change() {
return this._notify.asObservable().pipe(share(), filter((/**
* @param {?} ls
* @return {?}
*/
ls => ls.length !== 0)));
}
/**
* @return {?}
*/
clear() {
this.list = {};
this.cached = {};
}
/**
* @param {?} paths
* @return {?}
*/
load(paths) {
if (!Array.isArray(paths)) {
paths = [paths];
}
/** @type {?} */
const promises = [];
paths.forEach((/**
* @param {?} path
* @return {?}
*/
path => {
if (path.endsWith('.js')) {
promises.push(this.loadScript(path));
}
else {
promises.push(this.loadStyle(path));
}
}));
return Promise.all(promises).then((/**
* @param {?} res
* @return {?}
*/
res => {
this._notify.next(res);
return Promise.resolve(res);
}));
}
/**
* @param {?} path
* @param {?=} innerContent
* @return {?}
*/
loadScript(path, innerContent) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (this.list[path] === true) {
resolve(this.cached[path]);
return;
}
this.list[path] = true;
/** @type {?} */
const onSuccess = (/**
* @param {?} item
* @return {?}
*/
(item) => {
this.cached[path] = item;
resolve(item);
});
/** @type {?} */
const node = (/** @type {?} */ (this.doc.createElement('script')));
node.type = 'text/javascript';
node.src = path;
node.charset = 'utf-8';
if (innerContent) {
node.innerHTML = innerContent;
}
if (node.readyState) {
// IE
node.onreadystatechange = (/**
* @return {?}
*/
() => {
if (node.readyState === 'loaded' || node.readyState === 'complete') {
node.onreadystatechange = null;
onSuccess({
path,
loaded: true,
status: 'ok',
});
}
});
}
else {
node.onload = (/**
* @return {?}
*/
() => onSuccess({
path,
loaded: true,
status: 'ok',
}));
}
node.onerror = (/**
* @param {?} error
* @return {?}
*/
(error) => onSuccess({
path,
loaded: false,
status: 'error',
error,
}));
this.doc.getElementsByTagName('head')[0].appendChild(node);
}));
}
/**
* @param {?} path
* @param {?=} rel
* @param {?=} innerContent
* @return {?}
*/
loadStyle(path, rel = 'stylesheet', innerContent) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (this.list[path] === true) {
resolve(this.cached[path]);
return;
}
this.list[path] = true;
/** @type {?} */
const node = (/** @type {?} */ (this.doc.createElement('link')));
node.rel = rel;
node.type = 'text/css';
node.href = path;
if (innerContent) {
node.innerHTML = innerContent;
}
this.doc.getElementsByTagName('head')[0].appendChild(node);
/** @type {?} */
const item = {
path,
loaded: true,
status: 'ok',
};
this.cached[path] = item;
resolve(item);
}));
}
}
LazyService.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */
LazyService.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] }
];
/** @nocollapse */ LazyService.ɵprov = ɵɵdefineInjectable({ factory: function LazyService_Factory() { return new LazyService(ɵɵinject(DOCUMENT)); }, token: LazyService, providedIn: "root" });
if (false) {
/**
* @type {?}
* @private
*/
LazyService.prototype.list;
/**
* @type {?}
* @private
*/
LazyService.prototype.cached;
/**
* @type {?}
* @private
*/
LazyService.prototype._notify;
/**
* @type {?}
* @private
*/
LazyService.prototype.doc;
}
/**
* @fileoverview added by tsickle
* Generated from: src/validate/validate.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* 是否为数字
* @param {?} value
* @return {?}
*/
function isNum(value) {
return /^((-?\d+\.\d+)|(-?\d+)|(-?\.\d+))$/.test(value.toString());
}
/**
* 是否为整数
* @param {?} value
* @return {?}
*/
function isInt(value) {
return isNum(value) && parseInt(value.toString(), 10).toString() === value.toString();
}
/**
* 是否为小数
* @param {?} value
* @return {?}
*/
function isDecimal(value) {
return isNum(value) && !isInt(value);
}
/**
* 是否为身份证
* @param {?} value
* @return {?}
*/
function isIdCard(value) {
return typeof value === 'string' && /(^\d{15}$)|(^\d{17}([0-9]|X)$)/i.test(value);
}
/**
* 是否为手机号
* @param {?} value
* @return {?}
*/
function isMobile(value) {
return typeof value === 'string' && /^(0|\+?86|17951)?(13[0-9]|15[0-9]|17[0678]|18[0-9]|14[57])[0-9]{8}$/.test(value);
}
/**
* 是否URL地址
* @param {?} url
* @return {?}
*/
function isUrl(url) {
return /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/.test(url);
}
/**
* @fileoverview added by tsickle
* Generated from: src/validate/validators.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* 一套日常验证器
*/
// tslint:disable-next-line:class-name
class _Validators {
/**
* 是否为数字
* @param {?} control
* @return {?}
*/
static num(control) {
return isNum(control.value) ? null : { num: true };
}
/**
* 是否为整数
* @param {?} control
* @return {?}
*/
static int(control) {
return isInt(control.value) ? null : { int: true };
}
/**
* 是否为小数
* @param {?} control
* @return {?}
*/
static decimal(control) {
return isDecimal(control.value) ? null : { decimal: true };
}
/**
* 是否为身份证
* @param {?} control
* @return {?}
*/
static idCard(control) {
return isIdCard(control.value) ? null : { idCard: true };
}
/**
* 是否为手机号
* @param {?} control
* @return {?}
*/
static mobile(control) {
return isMobile(control.value) ? null : { mobile: true };
}
/**
* 是否URL地址
* @param {?} control
* @return {?}
*/
static url(control) {
return isUrl(control.value) ? null : { url: true };
}
}
/**
* @fileoverview added by tsickle
* Generated from: src/logger/logger.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const record = {};
/** @type {?} */
const PREFIX = '[@LXLIB]:';
/**
* @param {...?} args
* @return {?}
*/
function notRecorded(...args) {
/** @type {?} */
const asRecord = args.reduce((/**
* @param {?} acc
* @param {?} c
* @return {?}
*/
(acc, c) => acc + c.toString()), '');
if (record[asRecord]) {
return false;
}
else {
record[asRecord] = true;
return true;
}
}
/**
* @param {?} consoleFunc
* @param {...?} args
* @return {?}
*/
function consoleCommonBehavior(consoleFunc, ...args) {
if (environment.isTestMode || (isDevMode() && notRecorded(...args))) {
consoleFunc(...args);
}
}
// Warning should only be printed in dev mode and only once.
/** @type {?} */
const warn = (/**
* @param {...?} args
* @return {?}
*/
(...args) => consoleCommonBehavior((/**
* @param {...?} arg
* @return {?}
*/
(...arg) => console.warn(PREFIX, ...arg)), ...args));
/** @type {?} */
const warnDeprecation = (/**
* @param {...?} args
* @return {?}
*/
(...args) => {
if (!environment.isTestMode) {
/** @type {?} */
const stack = new Error().stack;
return consoleCommonBehavior((/**
* @param {...?} arg
* @return {?}
*/
(...arg) => console.warn(PREFIX, 'deprecated:', ...arg, stack)), ...args);
}
else {
return (/**
* @return {?}
*/
() => { });
}
});
// Log should only be printed in dev mode.
/** @type {?} */
const log = (/**
* @param {...?} args
* @return {?}
*/
(...args) => {
if (isDevMode()) {
console.log(PREFIX, ...args);
}
});
/**
* @fileoverview added by tsickle
* Generated from: src/other/check.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @param {?} element
* @return {?}
*/
function isEmpty(element) {
/** @type {?} */
const nodes = element.childNodes;
for (let i = 0; i < nodes.length; i++) {
/** @type {?} */
const node = nodes.item(i);
if (node.nodeType === 1 && ((/** @type {?} */ (node))).outerHTML.toString().trim().length !== 0) {
return false;
}
else if (node.nodeType === 3 && (/** @type {?} */ (node.textContent)).toString().trim().length !== 0) {
return false;
}
}
return true;
}
/**
* @template T, D
* @param {?} name
* @param {?} fallback
* @param {?} defaultValue
* @return {?}
*/
function propDecoratorFactory(name, fallback, defaultValue) {
/**
* @param {?} target
* @param {?} propName
* @param {?=} originalDescriptor
* @return {?}
*/
function propDecorator(target, propName, originalDescriptor) {
/** @type {?} */
const privatePropName = `$$__${propName}`;
if (Object.prototype.hasOwnProperty.call(target, privatePropName)) {
warn(`The prop "${privatePropName}" is already exist, it will be overrided by ${name} decorator.`);
}
Object.defineProperty(target, privatePropName, {
configurable: true,
writable: true,
});
return {
/**
* @return {?}
*/
get() {
return originalDescriptor && originalDescriptor.get ? originalDescriptor.get.bind(this)() : this[privatePropName];
},
/**
* @param {?} value
* @return {?}
*/
set(value) {
if (originalDescriptor && originalDescriptor.set) {
originalDescriptor.set.bind(this)(fallback(value, defaultValue));
}
this[privatePropName] = fallback(value, defaultValue);
},
};
}
return propDecorator;
}
/**
* @param {?} value
* @param {?=} allowUndefined
* @return {?}
*/
function toBoolean(value, allowUndefined = false) {
return allowUndefined && typeof value === 'undefined' ? undefined : value != null && `${value}` !== 'false';
}
/**
* Input decorator that handle a prop to do get/set automatically with toBoolean
*
* ```ts
* \@Input() InputBoolean() visible: boolean = false; / \@InputBoolean(null) visible: boolean = false;
* ```
* @param {?=} defaultValue
* @return {?}
*/
function InputBoolean(defaultValue = false) {
return propDecoratorFactory('InputNumber', toBoolean, defaultValue);
}
/**
* @param {?} value
* @param {?=} fallbackValue
* @return {?}
*/
function toNumber(value, fallbackValue = 0) {
return !isNaN(parseFloat((/** @type {?} */ (value)))) && !isNaN(Number(value)) ? Number(value) : fallbackValue;
}
/**
* Input decorator that handle a prop to do get/set automatically with toNumber
*
* ```ts
* \@Input() \@InputNumber() visible: number = 1; / \@InputNumber(null) visible: number = 2;
* ```
* @param {?=} defaultValue
* @return {?}
*/
function InputNumber(defaultValue = 0) {
return propDecoratorFactory('InputNumber', toNumber, defaultValue);
}
/**
* @fileoverview added by tsickle
* Generated from: src/other/style.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @param {?} el
* @param {?} classMap
* @param {?} renderer
* @return {?}
*/
function removeClass(el, classMap, renderer) {
// tslint:disable-next-line: forin
for (const i in classMap) {
renderer.removeClass(el, i);
}
}
/**
* @param {?} el
* @param {?} classMap
* @param {?} renderer
* @return {?}
*/
function addClass(el, classMap, renderer) {
for (const i in classMap) {
if (classMap[i]) {
renderer.addClass(el, i);
}
}
}
/**
* 更新宿主组件样式 `class`,例如:
*
* ```ts
* updateHostClass(
* this.el.nativeElement,
* this.renderer,
* {
* [ 'classname' ]: true,
* [ 'classname' ]: this.type === '1',
* [ this.cls ]: true,
* [ `a-${this.cls}` ]: true
* })
* ```
*
* @param {?} el
* @param {?} renderer
* @param {?} classMap
* @param {?=} cleanAll
* @return {?}
*/
function updateHostClass(el, renderer, classMap, cleanAll = false) {
if (cleanAll === true) {
renderer.removeAttribute(el, 'class');
}
else {
removeClass(el, classMap, renderer);
}
classMap = Object.assign({}, classMap);
addClass(el, classMap, renderer);
}
/**
* @fileoverview added by tsickle
* Generated from: src/array/array.config.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function ArrayConfig() { }
if (false) {
/**
* 深度项名,默认:`'deep'`
* @type {?|undefined}
*/
ArrayConfig.prototype.deepMapName;
/**
* 扁平后数组的父数据项名,默认:`'parent'`
* @type {?|undefined}
*/
ArrayConfig.prototype.parentMapName;
/**
* 编号项名,默认:`'id'`
* @type {?|undefined}
*/
ArrayConfig.prototype.idMapName;
/**
* 父编号项名,默认:`'parent_id'`
* @type {?|undefined}
*/
ArrayConfig.prototype.parentIdMapName;
/**
* 源数据子项名,默认:`'children'`
* @type {?|undefined}
*/
ArrayConfig.prototype.childrenMapName;
/**
* 标题项名,默认:`'title'`
* @type {?|undefined}
*/
ArrayConfig.prototype.titleMapName;
/**
* 节点 Checkbox 是否选中项名,默认:`'checked'`
* @type {?|undefined}
*/
ArrayConfig.prototype.checkedMapname;
/**
* 节点本身是否选中项名,默认:`'selected'`
* @type {?|undefined}
*/
ArrayConfig.prototype.selectedMapname;
/**
* 节点是否展开(叶子节点无效)项名,默认:`'expanded'`
* @type {?|undefined}
*/
ArrayConfig.prototype.expandedMapname;
/**
* 设置是否禁用节点(不可进行任何操作)项名,默认:`'disabled'`
* @type {?|undefined}
*/
ArrayConfig.prototype.disabledMapname;
}
/**
* @fileoverview added by tsickle
* Generated from: src/util.config.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class LxlibUtilConfig {
}
LxlibUtilConfig.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */ LxlibUtilConfig.ɵprov = ɵɵdefineInjectable({ factory: function LxlibUtilConfig_Factory() { return new LxlibUtilConfig(); }, token: LxlibUtilConfig, providedIn: "root" });
if (false) {
/** @type {?} */
LxlibUtilConfig.prototype.array;
}
/**
* @fileoverview added by tsickle
* Generated from: src/array/array.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function ArrayServiceTreeToArrOptions() { }
if (false) {
/**
* 深度项名,默认:`'deep'`
* @type {?|undefined}
*/
ArrayServiceTreeToArrOptions.prototype.deepMapName;
/**
* 扁平后数组的父数据项名,默认:`'parent'`
* @type {?|undefined}
*/
ArrayServiceTreeToArrOptions.prototype.parentMapName;
/**
* 源数据子项名,默认:`'children'`
* @type {?|undefined}
*/
ArrayServiceTreeToArrOptions.prototype.childrenMapName;
/**
* 是否移除 `children` 节点,默认:`true`
* @type {?|undefined}
*/
ArrayServiceTreeToArrOptions.prototype.clearChildren;
/**
* 转换成数组结构时回调
* @type {?|undefined}
*/
ArrayServiceTreeToArrOptions.prototype.cb;
}
/**
* @record
*/
function ArrayServiceArrToTreeOptions() { }
if (false) {
/**
* 编号项名,默认:`'id'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeOptions.prototype.idMapName;
/**
* 父编号项名,默认:`'parent_id'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeOptions.prototype.parentIdMapName;
/**
* 子项名,默认:`'children'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeOptions.prototype.childrenMapName;
/**
* 转换成树数据时回调
* @type {?|undefined}
*/
ArrayServiceArrToTreeOptions.prototype.cb;
}
/**
* @record
*/
function ArrayServiceArrToTreeNodeOptions() { }
if (false) {
/**
* 编号项名,默认:`'id'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.idMapName;
/**
* 父编号项名,默认:`'parent_id'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.parentIdMapName;
/**
* 标题项名,默认:`'title'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.titleMapName;
/**
* 设置为叶子节点项名,若数据源不存在时自动根据 `children` 值决定是否为叶子节点,默认:`'isLeaf'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.isLeafMapName;
/**
* 节点 Checkbox 是否选中项名,默认:`'checked'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.checkedMapname;
/**
* 节点本身是否选中项名,默认:`'selected'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.selectedMapname;
/**
* 节点是否展开(叶子节点无效)项名,默认:`'expanded'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.expandedMapname;
/**
* 设置是否禁用节点(不可进行任何操作)项名,默认:`'disabled'`
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.disabledMapname;
/**
* 转换成树数据后,执行的递归回调
* @type {?|undefined}
*/
ArrayServiceArrToTreeNodeOptions.prototype.cb;
}
/**
* @record
*/
function ArrayServiceGetKeysByTreeNodeOptions() { }
if (false) {
/**
* 是否包含半选状态的值,默认:`true`
* @type {?|undefined}
*/
ArrayServiceGetKeysByTreeNodeOptions.prototype.includeHalfChecked;
/**
* 是否重新指定 `key` 键名,若不指定表示使用 `NzTreeNode.key` 值
* @type {?|undefined}
*/
ArrayServiceGetKeysByTreeNodeOptions.prototype.keyMapName;
/**
* 回调,返回一个值 `key` 值,优先级高于其他
* @type {?|undefined}
*/
ArrayServiceGetKeysByTreeNodeOptions.prototype.cb;
}
class ArrayService {
/**
* @param {?} cog
*/
constructor(cog) {
this.c = Object.assign({ deepMapName: 'deep', parentMapName: 'parent', idMapName: 'id', parentIdMapName: 'parent_id', childrenMapName: 'children', titleMapName: 'title', checkedMapname: 'checked', selectedMapname: 'selected', expandedMapname: 'expanded', disabledMapname: 'disabled' }, (cog && cog.array));
}
/**
* 将树结构转换成数组结构
* @param {?} tree
* @param {?=} options
* @return {?}
*/
treeToArr(tree, options) {
/** @type {?} */
const opt = (/** @type {?} */ (Object.assign({ deepMapName: this.c.deepMapName, parentMapName: this.c.parentMapName, childrenMapName: this.c.childrenMapName, clearChildren: true, cb: null }, options)));
/** @type {?} */
const result = [];
/** @type {?} */
const inFn = (/**
* @param {?} list
* @param {?} parent
* @param {?=} deep
* @return {?}
*/
(list, parent, deep = 0) => {
for (const i of list) {
i[(/** @type {?} */ (opt.deepMapName))] = deep;
i[(/** @type {?} */ (opt.parentMapName))] = parent;
if (opt.cb) {
opt.cb(i, parent, deep);
}
result.push(i);
/** @type {?} */
const children = i[(/** @type {?} */ (opt.childrenMapName))];
if (children != null && Array.isArray(children) && children.length > 0) {
inFn(children, i, deep + 1);
}
if (opt.clearChildren) {
delete i[(/** @type {?} */ (opt.childrenMapName))];
}
}
});
inFn(tree, 1);
return result;
}
/**
* 数组转换成树数据
* @param {?} arr
* @param {?=} options
* @return {?}
*/
arrToTree(arr, options) {
/** @type {?} */
const opt = (/** @type {?} */ (Object.assign({ idMapName: this.c.idMapName, parentIdMapName: this.c.parentIdMapName, childrenMapName: this.c.childrenMapName, cb: null }, options)));
/** @type {?} */
const tree = [];
/** @type {?} */
const childrenOf = {};
for (const item of arr) {
/** @type {?} */
const id = item[(/** @type {?} */ (opt.idMapName))];
/** @type {?} */
const pid = item[(/** @type {?} */ (opt.parentIdMapName))];
childrenOf[id] = childrenOf[id] || [];
item[(/** @type {?} */ (opt.childrenMapName))] = childrenOf[id];
if (opt.cb) {
opt.cb(item);
}
if (pid) {
childrenOf[pid] = childrenOf[pid] || [];
childrenOf[pid].push(item);
}
else {
tree.push(item);
}
}
return tree;
}
/**
* 数组转换成 `nz-tree` 数据源,通过 `options` 转化项名,也可以使用 `options.cb` 更高级决定数据项
* @param {?} arr
* @param {?=} options
* @return {?}
*/
arrToTreeNode(arr, options) {
/** @type {?} */
const opt = (/** @type {?} */ (Object.assign({ idMapName: this.c.idMapName, parentIdMapName: this.c.parentIdMapName, titleMapName: this.c.titleMapName, isLeafMapName: 'isLeaf', checkedMapname: this.c.checkedMapname, selectedMapname: this.c.selectedMapname, expandedMapname: this.c.expandedMapname, disabledMapname: this.c.disabledMapname, cb: null }, options)));
/** @type {?} */
const tree = this.arrToTree(arr, {
idMapName: opt.idMapName,
parentIdMapName: opt.parentIdMapName,
childrenMapName: 'children',
});
this.visitTree(tree, (/**
* @param {?} item
* @param {?} parent
* @param {?} deep
* @return {?}
*/
(item, parent, deep) => {
item.key = item[(/** @type {?} */ (opt.idMapName))];
item.title = item[(/** @type {?} */ (opt.titleMapName))];
item.checked = item[(/** @type {?} */ (opt.checkedMapname))];
item.selected = item[(/** @type {?} */ (opt.selectedMapname))];
item.expanded = item[(/** @type {?} */ (opt.expandedMapname))];
item.disabled = item[(/** @type {?} */ (opt.disabledMapname))];
if (item[(/** @type {?} */ (opt.isLeafMapName))] == null) {
item.isLeaf = item.children.length === 0;
}
else {
item.isLeaf = item[(/** @type {?} */ (opt.isLeafMapName))];
}
if (opt.cb) {
opt.cb(item, parent, deep);
}
}));
return tree.map((/**
* @param {?} node
* @return {?}
*/
node => new NzTreeNode(node)));
}
/**
* 递归访问整个树
* @param {?} tree
* @param {?} cb
* @param {?=} options
* @return {?}
*/
visitTree(tree, cb, options) {
options = Object.assign({ childrenMapName: this.c.childrenMapName }, options);
/** @type {?} */
const inFn = (/**
* @param {?} data
* @param {?} parent
* @param {?} deep
* @return {?}
*/
(data, parent, deep) => {
for (const item of data) {
cb(item, parent, deep);
/** @type {?} */
const childrenVal = item[(/** @type {?} */ ((/** @type {?} */ (options)).childrenMapName))];
if (childrenVal && childrenVal.length > 0) {
inFn(childrenVal, item, deep + 1);
}
}
});
inFn(tree, null, 1);
}
/**
* 获取所有已经选中的 `key` 值
* @param {?} tree
* @param {?=} options
* @return {?}
*/
getKeysByTreeNode(tree, options) {
/** @type {?} */
const opt = (/** @type {?} */ (Object.assign({ includeHalfChecked: true }, options)));
/** @type {?} */
const keys = [];
this.visitTree(tree, (/**
* @param {?} item
* @param {?} parent
* @param {?} deep
* @return {?}
*/
(item, parent, deep) => {
if (item.isChecked || (opt.includeHalfChecked && item.isHalfChecked)) {
keys.push(opt.cb ? opt.cb(item, parent, deep) : opt.keyMapName ? item.origin[opt.keyMapName] : item.key);
}
}));
return keys;
}
}
ArrayService.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */
ArrayService.ctorParameters = () => [
{ type: LxlibUtilConfig }
];
/** @nocollapse */ ArrayService.ɵprov = ɵɵdefineInjectable({ factory: function ArrayService_Factory() { return new ArrayService(ɵɵinject(LxlibUtilConfig)); }, token: ArrayService, providedIn: "root" });
if (false) {
/**
* @type {?}
* @private
*/
ArrayService.prototype.c;
}
/**
* @fileoverview added by tsickle
* Generated from: src/util.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class LxlibUtilModule {
}
LxlibUtilModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: public_api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: util.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { ArrayService, InputBoolean, InputNumber, LazyService, LxlibUtilConfig, LxlibUtilModule, _Validators, copy, deepCopy, deepGet, deepMerge, deepMergeKey, fixEndTimeOfRange, format, getTimeDistance, isDecimal, isEmpty, isIdCard, isInt, isMobile, isNum, isUrl, toBoolean, toNumber, updateHostClass };
//# sourceMappingURL=util.js.map