diff --git a/src/base/1.pane.js b/src/base/1.pane.js index c87e33bfc..1f7f1cc59 100644 --- a/src/base/1.pane.js +++ b/src/base/1.pane.js @@ -6,7 +6,7 @@ * @extends BI.Widget * @abstract */ -import { Widget, shortcut } from "../core"; +import { Widget, shortcut, isNotEmptyString, extend, isNull, isEmpty } from "../core"; @shortcut() export default class Pane extends Widget { @@ -15,7 +15,7 @@ export default class Pane extends Widget { static EVENT_LOADING = "EVENT_LOADING"; _defaultConfig() { - return BI.extend(super._defaultConfig(), { + return extend(super._defaultConfig(), { _baseCls: "bi-pane", tipText: BI.i18nText("BI-No_Selected_Item"), loadingText: "", @@ -62,7 +62,7 @@ export default class Pane extends Widget { }); } BI.Layers.show(this.getName() + "-loading"); - } else if (BI.isNull(this._loading)) { + } else if (isNull(this._loading)) { loadingAnimation.element.css("zIndex", 1); BI.createWidget({ type: "bi.center_adapt", @@ -85,7 +85,7 @@ export default class Pane extends Widget { type: "bi.horizontal_adapt", items: [loadingTip], }]; - BI.isNotEmptyString(o.loadingText) && loadingTipItems.push({ + isNotEmptyString(o.loadingText) && loadingTipItems.push({ type: "bi.text", text: o.loadingText, tgap: this._getSize(10), @@ -109,7 +109,7 @@ export default class Pane extends Widget { } check() { - this.setTipVisible(BI.isEmpty(this.options.items)); + this.setTipVisible(isEmpty(this.options.items)); } setTipVisible(b) { diff --git a/src/core/2.base.js b/src/core/2.base.js index 7b200ed01..46bf10e92 100644 --- a/src/core/2.base.js +++ b/src/core/2.base.js @@ -21,127 +21,136 @@ var _applyFunc = function (name) { }; }; -// Utility -BI._.extend(BI, { - assert: function (v, is) { - if (this.isFunction(is)) { - if (!is(v)) { - throw new Error(v + " error"); - } else { - return true; - } - } - if (!this.isArray(is)) { - is = [is]; - } - if (!this.deepContains(is, v)) { +export function assert(v, is) { + if (isFunction(is)) { + if (!is(v)) { throw new Error(v + " error"); + } else { + return true; } - return true; - }, - - warn: function (message) { - console.warn(message); - }, - - UUID: function () { - var f = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]; - var str = ""; - for (var i = 0; i < 16; i++) { - var r = parseInt(f.length * Math.random(), 10); - str += f[r]; - } - return str; - }, + } + if (!isArray(is)) { + is = [is]; + } + if (!deepContains(is, v)) { + throw new Error(v + " error"); + } + return true; +} + +export function warn(message) { + console.warn(message); +} + +export function UUID() { + var f = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]; + var str = ""; + for (var i = 0; i < 16; i++) { + var r = _global.parseInt(f.length * Math.random(), 10); + str += f[r]; + } + return str; +} - isWidget: function (widget) { - return widget instanceof BI.Widget; - }, +export function isWidget(widget) { + return widget instanceof BI.Widget; +} - createWidgets: function (items, options, context) { - if (!BI.isArray(items)) { - throw new Error("items必须是数组", items); +export function createWidgets(items, options, context) { + if (!isArray(items)) { + throw new Error("items必须是数组", items); + } + if (isWidget(options)) { + context = options; + options = {}; + } else { + options || (options = {}); + } + return map(flatten(items), function (i, item) { + return BI.createWidget(item, deepClone(options), context); + }); +} + +export function createItems(data, innerAttr, outerAttr) { + innerAttr = isArray(innerAttr) ? innerAttr : makeArray(flatten(data).length, innerAttr || {}); + outerAttr = isArray(outerAttr) ? outerAttr : makeArray(flatten(data).length, outerAttr || {}); + return map(data, function (i, item) { + if (isArray(item)) { + return createItems(item, innerAttr, outerAttr); + } + if (item instanceof BI.Widget) { + return extend({}, innerAttr.shift(), outerAttr.shift(), { + type: null, + el: item + }); } - if (BI.isWidget(options)) { - context = options; - options = {}; - } else { - options || (options = {}); + if (innerAttr[0] instanceof BI.Widget) { + outerAttr.shift(); + return extend({}, item, { + el: innerAttr.shift() + }); } - return BI.map(BI.flatten(items), function (i, item) { - return BI.createWidget(item, BI.deepClone(options), context); - }); - }, - - createItems: function (data, innerAttr, outerAttr) { - innerAttr = BI.isArray(innerAttr) ? innerAttr : BI.makeArray(BI.flatten(data).length, innerAttr || {}); - outerAttr = BI.isArray(outerAttr) ? outerAttr : BI.makeArray(BI.flatten(data).length, outerAttr || {}); - return BI.map(data, function (i, item) { - if (BI.isArray(item)) { - return BI.createItems(item, innerAttr, outerAttr); - } - if (item instanceof BI.Widget) { - return BI.extend({}, innerAttr.shift(), outerAttr.shift(), { - type: null, - el: item - }); - } - if (innerAttr[0] instanceof BI.Widget) { - outerAttr.shift(); - return BI.extend({}, item, { - el: innerAttr.shift() - }); - } - if (item.el instanceof BI.Widget) { - innerAttr.shift(); - return BI.extend({}, outerAttr.shift(), { type: null }, item); - } - if (item.el) { - return BI.extend({}, outerAttr.shift(), item, { - el: BI.extend({}, innerAttr.shift(), item.el) - }); - } - return BI.extend({}, outerAttr.shift(), { - el: BI.extend({}, innerAttr.shift(), item) + if (item.el instanceof BI.Widget) { + innerAttr.shift(); + return extend({}, outerAttr.shift(), { type: null }, item); + } + if (item.el) { + return extend({}, outerAttr.shift(), item, { + el: extend({}, innerAttr.shift(), item.el) }); + } + return extend({}, outerAttr.shift(), { + el: extend({}, innerAttr.shift(), item) }); - }, - - // 用容器包装items - packageItems: function (items, layouts) { - for (var i = layouts.length - 1; i >= 0; i--) { - items = BI.map(items, function (k, it) { - return BI.extend({}, layouts[i], { - items: [ - BI.extend({}, layouts[i].el, { - el: it - }) - ] - }); + }); +} + +// 用容器包装items +export function packageItems(items, layouts) { + for (var i = layouts.length - 1; i >= 0; i--) { + items = map(items, function (k, it) { + return extend({}, layouts[i], { + items: [ + extend({}, layouts[i].el, { + el: it + }) + ] }); - } - return items; - }, + }); + } + return items; +} - formatEL: function (obj) { - if (obj && !obj.type && obj.el) { - return obj; - } - return { - el: obj - }; - }, +export function formatEL(obj) { + if (obj && !obj.type && obj.el) { + return obj; + } + return { + el: obj + }; +} - // 剥开EL - stripEL: function (obj) { - return obj.type && obj || obj.el || obj; - }, +// 剥开EL +export function stripEL(obj) { + return obj.type && obj || obj.el || obj; +} - trans2Element: function (widgets) { - return BI.map(widgets, function (i, wi) { - return wi.element; - }); - } +export function trans2Element(widgets) { + return map(widgets, (i, wi) => wi.element); +} + +// Utility +BI._.extend(BI, { + assert, + warn, + UUID, + isWidget, + createWidgets, + createItems, + packageItems, + formatEL, + stripEL, + trans2Element, }); // 集合相关方法 @@ -156,193 +165,242 @@ BI._.each(["get", "set", "each", "map", "reduce", "reduceRight", "find", "filter BI[name] = _applyFunc(name); } }); -BI._.extend(BI, { - // 数数 - count: function (from, to, predicate) { - var t; - if (predicate) { - for (t = from; t < to; t++) { - predicate(t); - } +export const where = BI.where; +export const findWhere = BI.findWhere; +export const invoke = BI.invoke; +export const pluck = BI.pluck; +export const shuffle = BI.shuffle; +export const sample = BI.sample; +export const toArray = BI.toArray; +export const size = BI.size; +export const get = BI.get; +export const set = BI.set; +export const each = BI.each; +export const map = BI.map; +export const reduce = BI.reduce; +export const reduceRight = BI.reduceRight; +export const find = BI.find; +export const filter = BI.filter; +export const reject = BI.reject; +export const every = BI.every; +export const all = BI.all; +export const some = BI.some; +export const any = BI.any; +export const max = BI.max; +export const min = BI.min; +export const sortBy = BI.sortBy; +export const groupBy = BI.groupBy; +export const indexBy = BI.indexBy; +export const countBy = BI.countBy; +export const partition = BI.partition; +export const clamp = BI.clamp; + +// 数数 +export function count(from, to, predicate) { + var t; + if (predicate) { + for (t = from; t < to; t++) { + predicate(t); } - return to - from; - }, - - // 倒数 - inverse: function (from, to, predicate) { - return BI.count(to, from, predicate); - }, - - firstKey: function (obj) { - var res = undefined; - BI.any(obj, function (key, value) { - res = key; - return true; - }); - return res; - }, - - lastKey: function (obj) { - var res = undefined; - BI.each(obj, function (key, value) { - res = key; - return true; - }); - return res; - }, + } + return to - from; +} + +// 倒数 +export function inverse(from, to, predicate) { + return count(to, from, predicate); +} + +export function firstKey(obj) { + let res = undefined; + any(obj, function (key, value) { + res = key; + return true; + }); + return res; +} + +export function lastKey(obj) { + let res = undefined; + each(obj, function (key, value) { + res = key; + return true; + }); + return res; +} + +export function firstObject(obj) { + let res = undefined; + any(obj, function (key, value) { + res = value; + return true; + }); + return res; +} + +export function lastObject(obj) { + let res = undefined; + each(obj, function (key, value) { + res = value; + return true; + }); + return res; +} + +export function concat(obj1, obj2) { + if (isKey(obj1)) { + return map([].slice.apply(arguments), function (idx, v) { + return v; + }).join(""); + } + if (isArray(obj1)) { + return BI._.concat.apply([], arguments); + } + if (isObject(obj1)) { + return extend.apply({}, arguments); + } +} - firstObject: function (obj) { - var res = undefined; - BI.any(obj, function (key, value) { - res = value; - return true; - }); - return res; - }, +export function backEach(obj, predicate, context) { + predicate = iteratee(predicate, context); + for (let index = obj.length - 1; index >= 0; index--) { + predicate(index, obj[index], obj); + } + return false; +} - lastObject: function (obj) { - var res = undefined; - BI.each(obj, function (key, value) { - res = value; +export function backAny(obj, predicate, context) { + predicate = iteratee(predicate, context); + for (let index = obj.length - 1; index >= 0; index--) { + if (predicate(index, obj[index], obj)) { return true; - }); - return res; - }, - - concat: function (obj1, obj2) { - if (BI.isKey(obj1)) { - return BI.map([].slice.apply(arguments), function (idx, v) { - return v; - }).join(""); - } - if (BI.isArray(obj1)) { - return BI._.concat.apply([], arguments); } - if (BI.isObject(obj1)) { - return BI._.extend.apply({}, arguments); - } - }, + } + return false; +} - backEach: function (obj, predicate, context) { - predicate = BI.iteratee(predicate, context); - for (var index = obj.length - 1; index >= 0; index--) { - predicate(index, obj[index], obj); +export function backEvery(obj, predicate, context) { + predicate = iteratee(predicate, context); + for (let index = obj.length - 1; index >= 0; index--) { + if (!predicate(index, obj[index], obj)) { + return false; } - return false; - }, + } + return true; +} - backAny: function (obj, predicate, context) { - predicate = BI.iteratee(predicate, context); - for (var index = obj.length - 1; index >= 0; index--) { - if (predicate(index, obj[index], obj)) { - return true; - } +export function backFindKey(obj, predicate, context) { + predicate = iteratee(predicate, context); + let objKeys = keys(obj), key; + for (let i = objKeys.length - 1; i >= 0; i--) { + key = objKeys[i]; + if (predicate(obj[key], key, obj)) { + return key; } - return false; - }, + } +} - backEvery: function (obj, predicate, context) { - predicate = BI.iteratee(predicate, context); - for (var index = obj.length - 1; index >= 0; index--) { - if (!predicate(index, obj[index], obj)) { - return false; - } - } - return true; - }, - - backFindKey: function (obj, predicate, context) { - predicate = BI.iteratee(predicate, context); - var keys = BI._.keys(obj), key; - for (var i = keys.length - 1; i >= 0; i--) { - key = keys[i]; - if (predicate(obj[key], key, obj)) { - return key; +export function backFind(obj, predicate, context) { + let key; + if (isArray(obj)) { + key = findLastIndex(obj, predicate, context); + } else { + key = backFindKey(obj, predicate, context); + } + if (key !== void 0 && key !== -1) { + return obj[key]; + } +} + +export function remove(obj, target, context) { + const targetIsFunction = isFunction(target); + target = targetIsFunction || isArray(target) ? target : [target]; + let i; + if (isArray(obj)) { + for (i = 0; i < obj.length; i++) { + if ((targetIsFunction && (target === obj[i] || target.apply(context, [i, obj[i]]) === true)) || (!targetIsFunction && contains(target, obj[i]))) { + obj.splice(i--, 1); } } - }, - - backFind: function (obj, predicate, context) { - var key; - if (BI.isArray(obj)) { - key = BI.findLastIndex(obj, predicate, context); - } else { - key = BI.backFindKey(obj, predicate, context); - } - if (key !== void 0 && key !== -1) { - return obj[key]; - } - }, - - remove: function (obj, target, context) { - var isFunction = BI.isFunction(target); - target = isFunction || BI.isArray(target) ? target : [target]; - var i; - if (BI.isArray(obj)) { - for (i = 0; i < obj.length; i++) { - if ((isFunction && (target === obj[i] || target.apply(context, [i, obj[i]]) === true)) || (!isFunction && BI.contains(target, obj[i]))) { - obj.splice(i--, 1); - } + } else { + each(obj, function (i, v) { + if ((targetIsFunction && (target === obj[i] || target.apply(context, [i, obj[i]]) === true)) || (!targetIsFunction && contains(target, obj[i]))) { + delete obj[i]; } + }); + } +} + +export function removeAt(obj, index) { + index = isArray(index) ? index : [index]; + let objIsArray = isArray(obj), i; + for (i = 0; i < index.length; i++) { + if (objIsArray) { + obj[index[i]] = "$deleteIndex"; } else { - BI.each(obj, function (i, v) { - if ((isFunction && (target === obj[i] || target.apply(context, [i, obj[i]]) === true)) || (!isFunction && BI.contains(target, obj[i]))) { - delete obj[i]; - } - }); - } - }, - - removeAt: function (obj, index) { - index = BI.isArray(index) ? index : [index]; - var isArray = BI.isArray(obj), i; - for (i = 0; i < index.length; i++) { - if (isArray) { - obj[index[i]] = "$deleteIndex"; - } else { - delete obj[index[i]]; - } - } - if (isArray) { - BI.remove(obj, "$deleteIndex"); + delete obj[index[i]]; } - }, - - string2Array: function (str) { - return str.split("&-&"); - }, - - array2String: function (array) { - return array.join("&-&"); - }, - - abc2Int: function (str) { - var idx = 0, start = "A", str = str.toUpperCase(); - for (var i = 0, len = str.length; i < len; ++i) { - idx = str.charAt(i).charCodeAt(0) - start.charCodeAt(0) + 26 * idx + 1; - if (idx > (2147483646 - str.charAt(i).charCodeAt(0) + start.charCodeAt(0)) / 26) { - return 0; - } - } - return idx; - }, - - int2Abc: function (num) { - var DIGITS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; - var idx = num, str = ""; - if (num === 0) { - return ""; + } + if (objIsArray) { + remove(obj, "$deleteIndex"); + } +} + +export function string2Array(str) { + return str.split("&-&"); +} + +export function array2String(array) { + return array.join("&-&"); +} + +export function abc2Int(string) { + let idx = 0, start = "A", str = string.toUpperCase(); + for (let i = 0, len = str.length; i < len; ++i) { + idx = str.charAt(i).charCodeAt(0) - start.charCodeAt(0) + 26 * idx + 1; + if (idx > (2147483646 - str.charAt(i).charCodeAt(0) + start.charCodeAt(0)) / 26) { + return 0; } - while (idx !== 0) { - var t = idx % 26; - if (t === 0) { - t = 26; - } - str = DIGITS[t - 1] + str; - idx = (idx - t) / 26; + } + return idx; +} + +export function int2Abc(num) { + const DIGITS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; + let idx = num, str = ""; + if (num === 0) { + return ""; + } + while (idx !== 0) { + let t = idx % 26; + if (t === 0) { + t = 26; } - return str; + str = DIGITS[t - 1] + str; + idx = (idx - t) / 26; } + return str; +} + +BI._.extend(BI, { + count, + inverse, + firstKey, + lastKey, + firstObject, + lastObject, + concat, + backEach, + backAny, + backEvery, + backFindKey, + backFind, + remove, + removeAt, + string2Array, + array2String, + abc2Int, + int2Abc, }); // 数组相关的方法 @@ -353,61 +411,89 @@ BI._.each(["first", "initial", "last", "rest", "compact", "flatten", "without", BI._.each(["findIndex", "findLastIndex"], function (name) { BI[name] = _applyFunc(name); }); -BI._.extend(BI, { - // 构建一个长度为length的数组 - makeArray: function (length, value) { - var res = []; - for (var i = 0; i < length; i++) { - if (BI.isNull(value)) { - res.push(i); - } else { - res.push(BI.deepClone(value)); - } +export const first = BI.first; +export const initial = BI.initial; +export const last = BI.last; +export const rest = BI.rest; +export const compact = BI.compact; +export const flatten = BI.flatten; +export const without = BI.without; +export const union = BI.union; +export const intersection = BI.intersection; +export const difference = BI.difference; +export const zip = BI.zip; +export const unzip = BI.unzip; +export const object = BI.object; +export const indexOf = BI.indexOf; +export const lastIndexOf = BI.lastIndexOf; +export const sortedIndex = BI.sortedIndex; +export const range = BI.range; +export const take = BI.take; +export const takeRight = BI.takeRight; +export const uniqBy = BI.uniqBy; +export const findIndex = BI.findIndex; +export const findLastIndex = BI.findLastIndex; + +// 构建一个长度为length的数组 +export function makeArray(length, value) { + let res = []; + for (let i = 0; i < length; i++) { + if (isNull(value)) { + res.push(i); + } else { + res.push(deepClone(value)); } - return res; - }, - - makeObject: function (array, value) { - var map = {}; - for (var i = 0; i < array.length; i++) { - if (BI.isNull(value)) { - map[array[i]] = array[i]; - } else if (BI.isFunction(value)) { - map[array[i]] = value(i, array[i]); - } else { - map[array[i]] = BI.deepClone(value); - } + } + return res; +} + +export function makeObject(array, value) { + let map = {}; + for (let i = 0; i < array.length; i++) { + if (isNull(value)) { + map[array[i]] = array[i]; + } else if (isFunction(value)) { + map[array[i]] = value(i, array[i]); + } else { + map[array[i]] = deepClone(value); } - return map; - }, + } + return map; +} - makeArrayByArray: function (array, value) { - var res = []; - if (!array) { - return res; - } - for (var i = 0, len = array.length; i < len; i++) { - if (BI.isArray(array[i])) { - res.push(BI.makeArrayByArray(array[i], value)); - } else { - res.push(BI.deepClone(value)); - } - } +export function makeArrayByArray(array, value) { + let res = []; + if (!array) { return res; - }, - - uniq: function (array, isSorted, iteratee, context) { - if (array == null) { - return []; - } - if (!BI._.isBoolean(isSorted)) { - context = iteratee; - iteratee = isSorted; - isSorted = false; + } + for (let i = 0, len = array.length; i < len; i++) { + if (isArray(array[i])) { + res.push(makeArrayByArray(array[i], value)); + } else { + res.push(deepClone(value)); } - iteratee && (iteratee = traverse(iteratee, context)); - return BI._.uniq.call(BI._, array, isSorted, iteratee, context); } + return res; +} + +export function uniq(array, isSorted, iteratee, context) { + if (array == null) { + return []; + } + if (!isBoolean(isSorted)) { + context = iteratee; + iteratee = isSorted; + isSorted = false; + } + iteratee && (iteratee = traverse(iteratee, context)); + return uniq.call(BI._, array, isSorted, iteratee, context); +} + +BI._.extend(BI, { + makeArray, + makeObject, + makeArrayByArray, + uniq, }); // 对象相关方法 @@ -417,244 +503,305 @@ BI._.each(["keys", "allKeys", "values", "pairs", "invert", "create", "functions" "isBoolean", "isDate", "isRegExp", "isError", "isNaN", "isUndefined", "zipObject", "cloneDeep"], function (name) { BI[name] = _apply(name); }); +export const keys = BI.keys; +export const allKeys = BI.allKeys; +export const values = BI.values; +export const pairs = BI.pairs; +export const invert = BI.invert; +export const create = BI.create; +export const functions = BI.functions; +export const extend = BI.extend; +export const extendOwn = BI.extendOwn; +export const defaults = BI.defaults; +export const clone = BI.clone; +export const property = BI.property; +export const propertyOf = BI.propertyOf; +export const matcher = BI.matcher; +export const isEqual = BI.isEqual; +export const isMatch = BI.isMatch; +export const isEmpty = BI.isEmpty; +export const isElement = BI.isElement; +export const isNumber = BI.isNumber; +export const isString = BI.isString; +export const isArray = BI.isArray; +export const isObject = BI.isObject; +export const isPlainObject = BI.isPlainObject; +export const isArguments = BI.isArguments; +export const isFunction = BI.isFunction; +export const isFinite = BI.isFinite; +export const isBoolean = BI.isBoolean; +export const isDate = BI.isDate; +export const isRegExp = BI.isRegExp; +export const isError = BI.isError; +export const isNaN = BI.isNaN; +export const isUndefined = BI.isUndefined; +export const zipObject = BI.zipObject; +export const cloneDeep = BI.cloneDeep; + BI._.each(["mapObject", "findKey", "pick", "omit", "tap"], function (name) { BI[name] = _applyFunc(name); }); -BI._.extend(BI, { - - inherit: function (sp, overrides) { - var sb = function () { - return sp.apply(this, arguments); - }; - var F = function () { - }, spp = sp.prototype; - F.prototype = spp; - sb.prototype = new F(); - sb.superclass = spp; - BI._.extend(sb.prototype, overrides, { - superclass: sp - }); - return sb; - }, - - init: function () { - // 先把准备环境准备好 - while (BI.prepares && BI.prepares.length > 0) { - BI.prepares.shift()(); - } - while (_global.___fineuiExposedFunction && _global.___fineuiExposedFunction.length > 0) { - _global.___fineuiExposedFunction.shift()(); - } - BI.initialized = true; - }, +export const mapObject = BI.mapObject; +export const findKey = BI.findKey; +export const pick = BI.pick; +export const omit = BI.omit; +export const tap = BI.tap; + +export function inherit(sp, overrides) { + let sb = function () { + return sp.apply(this, arguments); + }; + let F = function () { + }, spp = sp.prototype; + F.prototype = spp; + sb.prototype = new F(); + sb.superclass = spp; + extend(sb.prototype, overrides, { + superclass: sp + }); + return sb; +} + +export function init() { + // 先把准备环境准备好 + while (BI.prepares && BI.prepares.length > 0) { + BI.prepares.shift()(); + } + while (_global.___fineuiExposedFunction && _global.___fineuiExposedFunction.length > 0) { + _global.___fineuiExposedFunction.shift()(); + } + BI.initialized = true; +} - has: function (obj, keys) { - if (BI.isArray(keys)) { - if (keys.length === 0) { - return false; - } - return BI.every(keys, function (i, key) { - return BI._.has(obj, key); - }); - } - return BI._.has.apply(BI._, arguments); - }, - - freeze: function (value) { - // 在ES5中,如果这个方法的参数不是一个对象(一个原始值),那么它会导致 TypeError - // 在ES2015中,非对象参数将被视为要被冻结的普通对象,并被简单地返回 - if (Object.freeze && BI.isObject(value)) { - return Object.freeze(value); - } - return value; - }, - - // 数字和字符串可以作为key - isKey: function (key) { - return BI.isNumber(key) || (BI.isString(key) && key.length > 0); - }, - - // 忽略大小写的等于 - isCapitalEqual: function (a, b) { - a = BI.isNull(a) ? a : ("" + a).toLowerCase(); - b = BI.isNull(b) ? b : ("" + b).toLowerCase(); - return BI.isEqual(a, b); - }, - - isWidthOrHeight: function (w) { - if (typeof w === "number") { - return w >= 0; - } else if (typeof w === "string") { - return /^\d{1,3}(\.\d)?%$/.test(w) || w === "auto" || /^\d+(\.\d+)?px$/.test(w) || /^calc/.test(w); +export function has(obj, keys) { + if (isArray(keys)) { + if (keys.length === 0) { + return false; } - }, + return every(keys, function (i, key) { + return BI._.has(obj, key); + }); + } + return BI._.has.apply(BI._, arguments); +} + +export function freeze(value) { + // 在ES5中,如果这个方法的参数不是一个对象(一个原始值),那么它会导致 TypeError + // 在ES2015中,非对象参数将被视为要被冻结的普通对象,并被简单地返回 + if (Object.freeze && isObject(value)) { + return Object.freeze(value); + } + return value; +} + +// 数字和字符串可以作为key +export function isKey(key) { + return isNumber(key) || (isString(key) && key.length > 0); +} + +// 忽略大小写的等于 +export function isCapitalEqual(a, b) { + a = isNull(a) ? a : ("" + a).toLowerCase(); + b = isNull(b) ? b : ("" + b).toLowerCase(); + return isEqual(a, b); +} + +export function isWidthOrHeight(w) { + if (typeof w === "number") { + return w >= 0; + } else if (typeof w === "string") { + return /^\d{1,3}(\.\d)?%$/.test(w) || w === "auto" || /^\d+(\.\d+)?px$/.test(w) || /^calc/.test(w); + } +} - isNotNull: function (obj) { - return !BI.isNull(obj); - }, +export function isNotNull(obj) { + return !isNull(obj); +} - isNull: function (obj) { - return typeof obj === "undefined" || obj === null; - }, +export function isNull(obj) { + return typeof obj === "undefined" || obj === null; +} - isEmptyArray: function (arr) { - return BI.isArray(arr) && BI.isEmpty(arr); - }, +export function isEmptyArray(arr) { + return isArray(arr) && isEmpty(arr); +} - isNotEmptyArray: function (arr) { - return BI.isArray(arr) && !BI.isEmpty(arr); - }, +export function isNotEmptyArray(arr) { + return isArray(arr) && !isEmpty(arr); +} - isEmptyObject: function (obj) { - return BI.isEqual(obj, {}); - }, +export function isEmptyObject(obj) { + return isEqual(obj, {}); +} - isNotEmptyObject: function (obj) { - return BI.isPlainObject(obj) && !BI.isEmptyObject(obj); - }, +export function isNotEmptyObject(obj) { + return isPlainObject(obj) && !isEmptyObject(obj); +} - isEmptyString: function (obj) { - return BI.isString(obj) && obj.length === 0; - }, +export function isWindow(obj) { + return obj != null && obj == obj.window; +} - isNotEmptyString: function (obj) { - return BI.isString(obj) && !BI.isEmptyString(obj); - }, +export function isPromise(obj) { + return !!obj && (isObject(obj) || isFunction(obj)) && isFunction(obj.then); +} - isWindow: function (obj) { - return obj != null && obj == obj.window; - }, +BI._.extend(BI, { + inherit, + init, + has, + freeze, + isKey, + isCapitalEqual, + isWidthOrHeight, + isNotNull, + isNull, + isEmptyArray, + isNotEmptyArray, + isEmptyObject, + isNotEmptyObject, + isWindow, + isPromise, +}); - isPromise: function (obj) { - return !!obj && (BI.isObject(obj) || BI.isFunction(obj)) && BI.isFunction(obj.then); +export const deepClone = BI._.cloneDeep; +export const deepExtend = BI._.deepExtend; +export function isDeepMatch(object, attrs) { + let attrsKeys = keys(attrs), length = attrsKeys.length; + if (object == null) { + return !length; } -}); + const obj = Object(object); + for (let i = 0; i < length; i++) { + const key = attrsKeys[i]; + if (!isEqual(attrs[key], obj[key]) || !(key in obj)) { + return false; + } + } + return true; +} -// deep方法 -BI._.extend(BI, { - deepClone: BI._.cloneDeep, - deepExtend: BI._.merge, +export function contains(obj, target, fromIndex) { + if (!BI._.isArrayLike(obj)) obj = values(obj); + return indexOf(obj, target, typeof fromIndex === "number" && fromIndex) >= 0; +} - isDeepMatch: function (object, attrs) { - var keys = BI.keys(attrs), length = keys.length; - if (object == null) { - return !length; - } - var obj = Object(object); - for (var i = 0; i < length; i++) { - var key = keys[i]; - if (!BI.isEqual(attrs[key], obj[key]) || !(key in obj)) { - return false; - } - } - return true; - }, - - contains: function (obj, target, fromIndex) { - if (!BI._.isArrayLike(obj)) obj = BI._.values(obj); - return BI._.indexOf(obj, target, typeof fromIndex === "number" && fromIndex) >= 0; - }, - - deepContains: function (obj, copy) { - if (BI.isObject(copy)) { - return BI.any(obj, function (i, v) { - if (BI.isEqual(v, copy)) { - return true; - } - }); - } - return BI.contains(obj, copy); - }, +export function deepContains(obj, copy) { + if (isObject(copy)) { + return any(obj, (i, v) => isEqual(v, copy)); + } + return contains(obj, copy); +} - deepIndexOf: function (obj, target) { - for (var i = 0; i < obj.length; i++) { - if (BI.isEqual(target, obj[i])) { - return i; - } - } - return -1; - }, - - deepRemove: function (obj, target) { - var done = false; - var i; - if (BI.isArray(obj)) { - for (i = 0; i < obj.length; i++) { - if (BI.isEqual(target, obj[i])) { - obj.splice(i--, 1); - done = true; - } - } - } else { - BI.each(obj, function (i, v) { - if (BI.isEqual(target, obj[i])) { - delete obj[i]; - done = true; - } - }); +export function deepIndexOf(obj, target) { + for (let i = 0; i < obj.length; i++) { + if (isEqual(target, obj[i])) { + return i; } - return done; - }, - - deepWithout: function (obj, target) { - if (BI.isArray(obj)) { - var result = []; - for (var i = 0; i < obj.length; i++) { - if (!BI.isEqual(target, obj[i])) { - result.push(obj[i]); - } + } + return -1; +} + +export function deepRemove(obj, target) { + let done = false; + let i; + if (isArray(obj)) { + for (i = 0; i < obj.length; i++) { + if (isEqual(target, obj[i])) { + obj.splice(i--, 1); + done = true; } - return result; } - var result = {}; - BI.each(obj, function (i, v) { - if (!BI.isEqual(target, obj[i])) { - result[i] = v; - } - }); - return result; - - }, - - deepUnique: function (array) { - var result = []; - BI.each(array, function (i, item) { - if (!BI.deepContains(result, item)) { - result.push(item); + } else { + each(obj, (i, v) => { + if (isEqual(target, obj[i])) { + delete obj[i]; + done = true; } }); - return result; - }, - - // 比较两个对象得出不一样的key值 - deepDiff: function (object, other) { - object || (object = {}); - other || (other = {}); - var result = []; - var used = []; - for (var b in object) { - if (this.has(object, b)) { - if (!this.isEqual(object[b], other[b])) { - result.push(b); - } - used.push(b); + } + return done; +} + +export function deepWithout(obj, target) { + if (isArray(obj)) { + let result = []; + for (let i = 0; i < obj.length; i++) { + if (!isEqual(target, obj[i])) { + result.push(obj[i]); } } - for (var b in other) { - if (this.has(other, b) && !BI.contains(used, b)) { + return result; + } + let result = {}; + each(obj, (i, v) => { + if (!isEqual(target, obj[i])) { + result[i] = v; + } + }); + return result; +} + +export function deepUnique(array) { + let result = []; + each(array, (i, item) => { + if (!deepContains(result, item)) { + result.push(item); + } + }); + return result; +} + +// 比较两个对象得出不一样的key值 +export function deepDiff(object, other) { + object || (object = {}); + other || (other = {}); + let result = []; + let used = []; + for (let b in object) { + if (has(object, b)) { + if (!isEqual(object[b], other[b])) { result.push(b); } + used.push(b); + } + } + for (let b in other) { + if (has(other, b) && !contains(used, b)) { + result.push(b); } - return result; } + return result; +} + +// deep方法 +BI._.extend(BI, { + deepClone: BI._.cloneDeep, + deepExtend: BI._.merge, + isDeepMatch, + contains, + deepContains, + deepIndexOf, + deepRemove, + deepWithout, + deepUnique, + deepDiff, }); // 通用方法 -BI._.each(["uniqueId", "result", "chain", "iteratee", "escape", "unescape", "before", "after", "chunk"], function (name) { +BI._.each(["uniqueId", "result", "chain", "iteratee", "unescape", "before", "after", "chunk"], function (name) { BI[name] = function () { return BI._[name].apply(BI._, arguments); }; }); +export const uniqueId = BI.uniqueId; +export const result = BI.result; +export const chain = BI.chain; +export const iteratee = BI.iteratee; +export const unescape = BI.unescape; +export const before = BI.before; +export const after = BI.after; +export const chunk = BI.chunk; // 事件相关方法 BI._.each(["bind", "once", "partial", "debounce", "throttle", "delay", "defer", "wrap"], function (name) { @@ -662,627 +809,654 @@ BI._.each(["bind", "once", "partial", "debounce", "throttle", "delay", "defer", return BI._[name].apply(BI._, arguments); }; }); - -BI._.extend(BI, { - nextTick: (function () { - var callbacks = []; - var pending = false; - var timerFunc = void 0; - - function nextTickHandler() { - pending = false; - var copies = callbacks.slice(0); - callbacks.length = 0; - for (var i = 0; i < copies.length; i++) { - copies[i](); - } +export const bind = BI.bind; +export const once = BI.once; +export const partial = BI.partial; +export const debounce = BI.debounce; +export const throttle = BI.throttle; +export const delay = BI.delay; +export const defer = BI.defer; +export const wrap = BI.wrap; + +export const nextTick = (function () { + let callbacks = []; + let pending = false; + let timerFunc = void 0; + + function nextTickHandler() { + pending = false; + let copies = callbacks.slice(0); + callbacks.length = 0; + for (let i = 0; i < copies.length; i++) { + copies[i](); } + } - if (typeof Promise !== "undefined") { - var p = Promise.resolve(); - timerFunc = function timerFunc() { - p.then(nextTickHandler); - }; - } else if (typeof MutationObserver !== "undefined") { - var counter = 1; - var observer = new MutationObserver(nextTickHandler); - var textNode = document.createTextNode(String(counter)); - observer.observe(textNode, { - characterData: true - }); - timerFunc = function timerFunc() { - counter = (counter + 1) % 2; - textNode.data = String(counter); - }; - } else if (typeof setImmediate !== "undefined") { - timerFunc = function timerFunc() { - setImmediate(nextTickHandler); - }; - } else { - // Fallback to setTimeout. - timerFunc = function timerFunc() { - setTimeout(nextTickHandler, 0); - }; - } + if (typeof Promise !== "undefined") { + let p = Promise.resolve(); + timerFunc = function timerFunc() { + p.then(nextTickHandler); + }; + } else if (typeof MutationObserver !== "undefined") { + let counter = 1; + let observer = new MutationObserver(nextTickHandler); + let textNode = document.createTextNode(String(counter)); + observer.observe(textNode, { + characterData: true + }); + timerFunc = function timerFunc() { + counter = (counter + 1) % 2; + textNode.data = String(counter); + }; + } else if (typeof setImmediate !== "undefined") { + timerFunc = function timerFunc() { + setImmediate(nextTickHandler); + }; + } else { + // Fallback to setTimeout. + timerFunc = function timerFunc() { + setTimeout(nextTickHandler, 0); + }; + } - return function queueNextTick(cb) { - var _resolve = void 0; - var args = [].slice.call(arguments, 1); - callbacks.push(function () { - if (cb) { - try { - cb.apply(null, args); - } catch (e) { - console.error(e); - } - } else if (_resolve) { - _resolve.apply(null, args); + return function queueNextTick(cb) { + let _resolve = void 0; + let args = [].slice.call(arguments, 1); + callbacks.push(function () { + if (cb) { + try { + cb.apply(null, args); + } catch (e) { + console.error(e); } - }); - if (!pending) { - pending = true; - timerFunc(); - } - // $flow-disable-line - if (!cb && typeof Promise !== 'undefined') { - return new Promise(function (resolve, reject) { - _resolve = resolve; - }); + } else if (_resolve) { + _resolve.apply(null, args); } - }; - })() -}); + }); + if (!pending) { + pending = true; + timerFunc(); + } + // $flow-disable-line + if (!cb && typeof Promise !== 'undefined') { + return new Promise(function (resolve, reject) { + _resolve = resolve; + }); + } + }; +})(); + +BI._.extend(BI, { nextTick }); // 数字相关方法 BI._.each(["random"], function (name) { BI[name] = _apply(name); }); -BI._.extend(BI, { +export const random = BI.random; - parseInt: function (number) { - var radix = 10; - if (/^0x/g.test(number)) { - radix = 16; - } - try { - return parseInt(number, radix); - } catch (e) { - throw new Error(number + "parse int error"); - return NaN; - } - }, - - parseSafeInt: function (value) { - var MAX_SAFE_INTEGER = 9007199254740991; - return value - ? this.clamp(this.parseInt(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER) - : (value === 0 ? value : 0); - }, - - parseFloat: function (number) { - try { - return parseFloat(number); - } catch (e) { - throw new Error(number + "parse float error"); - return NaN; - } - }, +export function parseInt(number) { + let radix = 10; + if (/^0x/g.test(number)) { + radix = 16; + } + try { + return _global.parseInt(number, radix); + } catch (e) { + throw new Error(number + "parse int error"); + return NaN; + } +} + +export function parseSafeInt(value) { + let MAX_SAFE_INTEGER = 9007199254740991; + return value + ? clamp(parseInt(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER) + : (value === 0 ? value : 0); +} + +export function parseFloat(number) { + try { + return _global.parseFloat(number); + } catch (e) { + throw new Error(number + "parse float error"); + return NaN; + } +} - isNaturalNumber: function (number) { - if (/^\d+$/.test(number)) { - return true; - } - return false; - }, +export function isNaturalNumber(number) { + return /^\d+$/.test(number); +} - isPositiveInteger: function (number) { - if (/^\+?[1-9][0-9]*$/.test(number)) { - return true; - } - return false; - }, +export function isPositiveInteger(number) { + return /^\+?[1-9][0-9]*$/.test(number); +} - isNegativeInteger: function (number) { - if (/^\-[1-9][0-9]*$/.test(number)) { - return true; - } - return false; - }, +export function isNegativeInteger(number) { + return /^\-[1-9][0-9]*$/.test(number); +} - isInteger: function (number) { - if (/^\-?\d+$/.test(number)) { - return true; - } - return false; - }, +export function isInteger(number) { + return /^\-?\d+$/.test(number); +} - isNumeric: function (number) { - return !isNaN(parseFloat(number)) && isFinite(number); - }, +export function isNumeric(number) { + return !_global.isNaN(_global.parseFloat(number)) && _global.isFinite(number); +} - isFloat: function (number) { - if (/^([+-]?)\d*\.\d+$/.test(number)) { - return true; - } +export function isFloat(number) { + return /^([+-]?)\d*\.\d+$/.test(number); +} + +export function isOdd(number) { + if (!isInteger(number)) { return false; - }, + } + return (number & 1) === 1; +} - isOdd: function (number) { - if (!BI.isInteger(number)) { - return false; +export function isEven(number) { + if (!isInteger(number)) { + return false; + } + return (number & 1) === 0; +} + +export function sum(array, iteratee, context) { + let sum = 0; + each(array, (i, item) => { + if (iteratee) { + sum += Number(iteratee.apply(context, [i, item])); + } else { + sum += Number(item); } - return (number & 1) === 1; - }, + }); + return sum; +} - isEven: function (number) { - if (!BI.isInteger(number)) { - return false; - } - return (number & 1) === 0; - }, - - sum: function (array, iteratee, context) { - var sum = 0; - BI.each(array, function (i, item) { - if (iteratee) { - sum += Number(iteratee.apply(context, [i, item])); - } else { - sum += Number(item); - } - }); - return sum; - }, +export function average(array, iteratee, context) { + const sumResult = sum(array, iteratee, context); + return sumResult / array.length; +} - average: function (array, iteratee, context) { - var sum = BI.sum(array, iteratee, context); - return sum / array.length; - } +BI._.extend(BI, { + parseInt, + parseSafeInt, + parseFloat, + isNaturalNumber, + isPositiveInteger, + isNegativeInteger, + isInteger, + isNumeric, + isFloat, + isOdd, + isEven, + sum, + average, }); +export function trim() { + return BI._.trim.apply(BI._, arguments); +} + +export function toUpperCase(string) { + return (string + "").toLocaleUpperCase(); +} + +export function toLowerCase(string) { + return (string + "").toLocaleLowerCase(); +} + +export function isEndWithBlank(string) { + return /(\s|\u00A0)$/.test(string); +} + +export function isLiteral(exp) { + return /^\s?(true|false|-?[\d\.]+|'[^']*'|"[^"]*")\s?$/.test(exp); +} + +export function stripQuotes(str) { + const a = str.charCodeAt(0); + const b = str.charCodeAt(str.length - 1); + return a === b && (a === 0x22 || a === 0x27) + ? str.slice(1, -1) + : str; +} + +// background-color => backgroundColor +export function camelize(str) { + return str.replace(/-(.)/g, (_, character) => character.toUpperCase()); +} + +// backgroundColor => background-color +export function hyphenate(str) { + return str.replace(/([A-Z])/g, "-$1").toLowerCase(); +} + +export function isNotEmptyString(str) { + return isString(str) && !isEmpty(str); +} + +export function isEmptyString(str) { + return isString(str) && isEmpty(str); +} + +/** + * 通用解密方法 + * @param type 解密方式 + * @param text 文本 + * @param key 种子 + * @return {*} + */ +export function encrypt(type, text, key) { + switch (type) { + case BI.CRYPT_TYPE.AES: + default: + return BI.aesEncrypt(text, key); + } +} + +/** + * 通用解密方法 + * @param type 解密方式 + * @param text 文本 + * @param key 种子 + * @return {*} + */ +export function decrypt(type, text, key) { + switch (type) { + case BI.CRYPT_TYPE.AES: + default: + return BI.aesDecrypt(text, key); + } +} + +/** + * 对字符串中的'和\做编码处理 + * @static + * @param {String} string 要做编码处理的字符串 + * @return {String} 编码后的字符串 + */ +export function escape(string) { + return string.replace(/('|\\)/g, "\\$1"); +} + + +/** + * 让字符串通过指定字符做补齐的函数 + * + * var s = BI.leftPad('123', 5, '0');//s的值为:'00123' + * + * @static + * @param {String} val 原始值 + * @param {Number} size 总共需要的位数 + * @param {String} ch 用于补齐的字符 + * @return {String} 补齐后的字符串 + */ +export function leftPad(val, size, ch) { + let result = String(val); + if (!ch) { + ch = " "; + } + while (result.length < size) { + result = ch + result; + } + return result.toString(); +} + +/** + * 对字符串做替换的函数 + * + * var cls = 'my-class', text = 'Some text'; + * var res = BI.format('