util.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import { platformApi } from './platform.js';
  2. var BUILTIN_OBJECT = reduce([
  3. 'Function',
  4. 'RegExp',
  5. 'Date',
  6. 'Error',
  7. 'CanvasGradient',
  8. 'CanvasPattern',
  9. 'Image',
  10. 'Canvas'
  11. ], function (obj, val) {
  12. obj['[object ' + val + ']'] = true;
  13. return obj;
  14. }, {});
  15. var TYPED_ARRAY = reduce([
  16. 'Int8',
  17. 'Uint8',
  18. 'Uint8Clamped',
  19. 'Int16',
  20. 'Uint16',
  21. 'Int32',
  22. 'Uint32',
  23. 'Float32',
  24. 'Float64'
  25. ], function (obj, val) {
  26. obj['[object ' + val + 'Array]'] = true;
  27. return obj;
  28. }, {});
  29. var objToString = Object.prototype.toString;
  30. var arrayProto = Array.prototype;
  31. var nativeForEach = arrayProto.forEach;
  32. var nativeFilter = arrayProto.filter;
  33. var nativeSlice = arrayProto.slice;
  34. var nativeMap = arrayProto.map;
  35. var ctorFunction = function () { }.constructor;
  36. var protoFunction = ctorFunction ? ctorFunction.prototype : null;
  37. var protoKey = '__proto__';
  38. var idStart = 0x0907;
  39. export function guid() {
  40. return idStart++;
  41. }
  42. export function logError() {
  43. var args = [];
  44. for (var _i = 0; _i < arguments.length; _i++) {
  45. args[_i] = arguments[_i];
  46. }
  47. if (typeof console !== 'undefined') {
  48. console.error.apply(console, args);
  49. }
  50. }
  51. export function clone(source) {
  52. if (source == null || typeof source !== 'object') {
  53. return source;
  54. }
  55. var result = source;
  56. var typeStr = objToString.call(source);
  57. if (typeStr === '[object Array]') {
  58. if (!isPrimitive(source)) {
  59. result = [];
  60. for (var i = 0, len = source.length; i < len; i++) {
  61. result[i] = clone(source[i]);
  62. }
  63. }
  64. }
  65. else if (TYPED_ARRAY[typeStr]) {
  66. if (!isPrimitive(source)) {
  67. var Ctor = source.constructor;
  68. if (Ctor.from) {
  69. result = Ctor.from(source);
  70. }
  71. else {
  72. result = new Ctor(source.length);
  73. for (var i = 0, len = source.length; i < len; i++) {
  74. result[i] = source[i];
  75. }
  76. }
  77. }
  78. }
  79. else if (!BUILTIN_OBJECT[typeStr] && !isPrimitive(source) && !isDom(source)) {
  80. result = {};
  81. for (var key in source) {
  82. if (source.hasOwnProperty(key) && key !== protoKey) {
  83. result[key] = clone(source[key]);
  84. }
  85. }
  86. }
  87. return result;
  88. }
  89. export function merge(target, source, overwrite) {
  90. if (!isObject(source) || !isObject(target)) {
  91. return overwrite ? clone(source) : target;
  92. }
  93. for (var key in source) {
  94. if (source.hasOwnProperty(key) && key !== protoKey) {
  95. var targetProp = target[key];
  96. var sourceProp = source[key];
  97. if (isObject(sourceProp)
  98. && isObject(targetProp)
  99. && !isArray(sourceProp)
  100. && !isArray(targetProp)
  101. && !isDom(sourceProp)
  102. && !isDom(targetProp)
  103. && !isBuiltInObject(sourceProp)
  104. && !isBuiltInObject(targetProp)
  105. && !isPrimitive(sourceProp)
  106. && !isPrimitive(targetProp)) {
  107. merge(targetProp, sourceProp, overwrite);
  108. }
  109. else if (overwrite || !(key in target)) {
  110. target[key] = clone(source[key]);
  111. }
  112. }
  113. }
  114. return target;
  115. }
  116. export function mergeAll(targetAndSources, overwrite) {
  117. var result = targetAndSources[0];
  118. for (var i = 1, len = targetAndSources.length; i < len; i++) {
  119. result = merge(result, targetAndSources[i], overwrite);
  120. }
  121. return result;
  122. }
  123. export function extend(target, source) {
  124. if (Object.assign) {
  125. Object.assign(target, source);
  126. }
  127. else {
  128. for (var key in source) {
  129. if (source.hasOwnProperty(key) && key !== protoKey) {
  130. target[key] = source[key];
  131. }
  132. }
  133. }
  134. return target;
  135. }
  136. export function defaults(target, source, overlay) {
  137. var keysArr = keys(source);
  138. for (var i = 0; i < keysArr.length; i++) {
  139. var key = keysArr[i];
  140. if ((overlay ? source[key] != null : target[key] == null)) {
  141. target[key] = source[key];
  142. }
  143. }
  144. return target;
  145. }
  146. export var createCanvas = platformApi.createCanvas;
  147. export function indexOf(array, value) {
  148. if (array) {
  149. if (array.indexOf) {
  150. return array.indexOf(value);
  151. }
  152. for (var i = 0, len = array.length; i < len; i++) {
  153. if (array[i] === value) {
  154. return i;
  155. }
  156. }
  157. }
  158. return -1;
  159. }
  160. export function inherits(clazz, baseClazz) {
  161. var clazzPrototype = clazz.prototype;
  162. function F() { }
  163. F.prototype = baseClazz.prototype;
  164. clazz.prototype = new F();
  165. for (var prop in clazzPrototype) {
  166. if (clazzPrototype.hasOwnProperty(prop)) {
  167. clazz.prototype[prop] = clazzPrototype[prop];
  168. }
  169. }
  170. clazz.prototype.constructor = clazz;
  171. clazz.superClass = baseClazz;
  172. }
  173. export function mixin(target, source, override) {
  174. target = 'prototype' in target ? target.prototype : target;
  175. source = 'prototype' in source ? source.prototype : source;
  176. if (Object.getOwnPropertyNames) {
  177. var keyList = Object.getOwnPropertyNames(source);
  178. for (var i = 0; i < keyList.length; i++) {
  179. var key = keyList[i];
  180. if (key !== 'constructor') {
  181. if ((override ? source[key] != null : target[key] == null)) {
  182. target[key] = source[key];
  183. }
  184. }
  185. }
  186. }
  187. else {
  188. defaults(target, source, override);
  189. }
  190. }
  191. export function isArrayLike(data) {
  192. if (!data) {
  193. return false;
  194. }
  195. if (typeof data === 'string') {
  196. return false;
  197. }
  198. return typeof data.length === 'number';
  199. }
  200. export function each(arr, cb, context) {
  201. if (!(arr && cb)) {
  202. return;
  203. }
  204. if (arr.forEach && arr.forEach === nativeForEach) {
  205. arr.forEach(cb, context);
  206. }
  207. else if (arr.length === +arr.length) {
  208. for (var i = 0, len = arr.length; i < len; i++) {
  209. cb.call(context, arr[i], i, arr);
  210. }
  211. }
  212. else {
  213. for (var key in arr) {
  214. if (arr.hasOwnProperty(key)) {
  215. cb.call(context, arr[key], key, arr);
  216. }
  217. }
  218. }
  219. }
  220. export function map(arr, cb, context) {
  221. if (!arr) {
  222. return [];
  223. }
  224. if (!cb) {
  225. return slice(arr);
  226. }
  227. if (arr.map && arr.map === nativeMap) {
  228. return arr.map(cb, context);
  229. }
  230. else {
  231. var result = [];
  232. for (var i = 0, len = arr.length; i < len; i++) {
  233. result.push(cb.call(context, arr[i], i, arr));
  234. }
  235. return result;
  236. }
  237. }
  238. export function reduce(arr, cb, memo, context) {
  239. if (!(arr && cb)) {
  240. return;
  241. }
  242. for (var i = 0, len = arr.length; i < len; i++) {
  243. memo = cb.call(context, memo, arr[i], i, arr);
  244. }
  245. return memo;
  246. }
  247. export function filter(arr, cb, context) {
  248. if (!arr) {
  249. return [];
  250. }
  251. if (!cb) {
  252. return slice(arr);
  253. }
  254. if (arr.filter && arr.filter === nativeFilter) {
  255. return arr.filter(cb, context);
  256. }
  257. else {
  258. var result = [];
  259. for (var i = 0, len = arr.length; i < len; i++) {
  260. if (cb.call(context, arr[i], i, arr)) {
  261. result.push(arr[i]);
  262. }
  263. }
  264. return result;
  265. }
  266. }
  267. export function find(arr, cb, context) {
  268. if (!(arr && cb)) {
  269. return;
  270. }
  271. for (var i = 0, len = arr.length; i < len; i++) {
  272. if (cb.call(context, arr[i], i, arr)) {
  273. return arr[i];
  274. }
  275. }
  276. }
  277. export function keys(obj) {
  278. if (!obj) {
  279. return [];
  280. }
  281. if (Object.keys) {
  282. return Object.keys(obj);
  283. }
  284. var keyList = [];
  285. for (var key in obj) {
  286. if (obj.hasOwnProperty(key)) {
  287. keyList.push(key);
  288. }
  289. }
  290. return keyList;
  291. }
  292. function bindPolyfill(func, context) {
  293. var args = [];
  294. for (var _i = 2; _i < arguments.length; _i++) {
  295. args[_i - 2] = arguments[_i];
  296. }
  297. return function () {
  298. return func.apply(context, args.concat(nativeSlice.call(arguments)));
  299. };
  300. }
  301. export var bind = (protoFunction && isFunction(protoFunction.bind))
  302. ? protoFunction.call.bind(protoFunction.bind)
  303. : bindPolyfill;
  304. function curry(func) {
  305. var args = [];
  306. for (var _i = 1; _i < arguments.length; _i++) {
  307. args[_i - 1] = arguments[_i];
  308. }
  309. return function () {
  310. return func.apply(this, args.concat(nativeSlice.call(arguments)));
  311. };
  312. }
  313. export { curry };
  314. export function isArray(value) {
  315. if (Array.isArray) {
  316. return Array.isArray(value);
  317. }
  318. return objToString.call(value) === '[object Array]';
  319. }
  320. export function isFunction(value) {
  321. return typeof value === 'function';
  322. }
  323. export function isString(value) {
  324. return typeof value === 'string';
  325. }
  326. export function isStringSafe(value) {
  327. return objToString.call(value) === '[object String]';
  328. }
  329. export function isNumber(value) {
  330. return typeof value === 'number';
  331. }
  332. export function isObject(value) {
  333. var type = typeof value;
  334. return type === 'function' || (!!value && type === 'object');
  335. }
  336. export function isBuiltInObject(value) {
  337. return !!BUILTIN_OBJECT[objToString.call(value)];
  338. }
  339. export function isTypedArray(value) {
  340. return !!TYPED_ARRAY[objToString.call(value)];
  341. }
  342. export function isDom(value) {
  343. return typeof value === 'object'
  344. && typeof value.nodeType === 'number'
  345. && typeof value.ownerDocument === 'object';
  346. }
  347. export function isGradientObject(value) {
  348. return value.colorStops != null;
  349. }
  350. export function isImagePatternObject(value) {
  351. return value.image != null;
  352. }
  353. export function isRegExp(value) {
  354. return objToString.call(value) === '[object RegExp]';
  355. }
  356. export function eqNaN(value) {
  357. return value !== value;
  358. }
  359. export function retrieve() {
  360. var args = [];
  361. for (var _i = 0; _i < arguments.length; _i++) {
  362. args[_i] = arguments[_i];
  363. }
  364. for (var i = 0, len = args.length; i < len; i++) {
  365. if (args[i] != null) {
  366. return args[i];
  367. }
  368. }
  369. }
  370. export function retrieve2(value0, value1) {
  371. return value0 != null
  372. ? value0
  373. : value1;
  374. }
  375. export function retrieve3(value0, value1, value2) {
  376. return value0 != null
  377. ? value0
  378. : value1 != null
  379. ? value1
  380. : value2;
  381. }
  382. export function slice(arr) {
  383. var args = [];
  384. for (var _i = 1; _i < arguments.length; _i++) {
  385. args[_i - 1] = arguments[_i];
  386. }
  387. return nativeSlice.apply(arr, args);
  388. }
  389. export function normalizeCssArray(val) {
  390. if (typeof (val) === 'number') {
  391. return [val, val, val, val];
  392. }
  393. var len = val.length;
  394. if (len === 2) {
  395. return [val[0], val[1], val[0], val[1]];
  396. }
  397. else if (len === 3) {
  398. return [val[0], val[1], val[2], val[1]];
  399. }
  400. return val;
  401. }
  402. export function assert(condition, message) {
  403. if (!condition) {
  404. throw new Error(message);
  405. }
  406. }
  407. export function trim(str) {
  408. if (str == null) {
  409. return null;
  410. }
  411. else if (typeof str.trim === 'function') {
  412. return str.trim();
  413. }
  414. else {
  415. return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  416. }
  417. }
  418. var primitiveKey = '__ec_primitive__';
  419. export function setAsPrimitive(obj) {
  420. obj[primitiveKey] = true;
  421. }
  422. export function isPrimitive(obj) {
  423. return obj[primitiveKey];
  424. }
  425. var MapPolyfill = (function () {
  426. function MapPolyfill() {
  427. this.data = {};
  428. }
  429. MapPolyfill.prototype["delete"] = function (key) {
  430. var existed = this.has(key);
  431. if (existed) {
  432. delete this.data[key];
  433. }
  434. return existed;
  435. };
  436. MapPolyfill.prototype.has = function (key) {
  437. return this.data.hasOwnProperty(key);
  438. };
  439. MapPolyfill.prototype.get = function (key) {
  440. return this.data[key];
  441. };
  442. MapPolyfill.prototype.set = function (key, value) {
  443. this.data[key] = value;
  444. return this;
  445. };
  446. MapPolyfill.prototype.keys = function () {
  447. return keys(this.data);
  448. };
  449. MapPolyfill.prototype.forEach = function (callback) {
  450. var data = this.data;
  451. for (var key in data) {
  452. if (data.hasOwnProperty(key)) {
  453. callback(data[key], key);
  454. }
  455. }
  456. };
  457. return MapPolyfill;
  458. }());
  459. var isNativeMapSupported = typeof Map === 'function';
  460. function maybeNativeMap() {
  461. return (isNativeMapSupported ? new Map() : new MapPolyfill());
  462. }
  463. var HashMap = (function () {
  464. function HashMap(obj) {
  465. var isArr = isArray(obj);
  466. this.data = maybeNativeMap();
  467. var thisMap = this;
  468. (obj instanceof HashMap)
  469. ? obj.each(visit)
  470. : (obj && each(obj, visit));
  471. function visit(value, key) {
  472. isArr ? thisMap.set(value, key) : thisMap.set(key, value);
  473. }
  474. }
  475. HashMap.prototype.hasKey = function (key) {
  476. return this.data.has(key);
  477. };
  478. HashMap.prototype.get = function (key) {
  479. return this.data.get(key);
  480. };
  481. HashMap.prototype.set = function (key, value) {
  482. this.data.set(key, value);
  483. return value;
  484. };
  485. HashMap.prototype.each = function (cb, context) {
  486. this.data.forEach(function (value, key) {
  487. cb.call(context, value, key);
  488. });
  489. };
  490. HashMap.prototype.keys = function () {
  491. var keys = this.data.keys();
  492. return isNativeMapSupported
  493. ? Array.from(keys)
  494. : keys;
  495. };
  496. HashMap.prototype.removeKey = function (key) {
  497. this.data["delete"](key);
  498. };
  499. return HashMap;
  500. }());
  501. export { HashMap };
  502. export function createHashMap(obj) {
  503. return new HashMap(obj);
  504. }
  505. export function concatArray(a, b) {
  506. var newArray = new a.constructor(a.length + b.length);
  507. for (var i = 0; i < a.length; i++) {
  508. newArray[i] = a[i];
  509. }
  510. var offset = a.length;
  511. for (var i = 0; i < b.length; i++) {
  512. newArray[i + offset] = b[i];
  513. }
  514. return newArray;
  515. }
  516. export function createObject(proto, properties) {
  517. var obj;
  518. if (Object.create) {
  519. obj = Object.create(proto);
  520. }
  521. else {
  522. var StyleCtor = function () { };
  523. StyleCtor.prototype = proto;
  524. obj = new StyleCtor();
  525. }
  526. if (properties) {
  527. extend(obj, properties);
  528. }
  529. return obj;
  530. }
  531. export function disableUserSelect(dom) {
  532. var domStyle = dom.style;
  533. domStyle.webkitUserSelect = 'none';
  534. domStyle.userSelect = 'none';
  535. domStyle.webkitTapHighlightColor = 'rgba(0,0,0,0)';
  536. domStyle['-webkit-touch-callout'] = 'none';
  537. }
  538. export function hasOwn(own, prop) {
  539. return own.hasOwnProperty(prop);
  540. }
  541. export function noop() { }
  542. export var RADIAN_TO_DEGREE = 180 / Math.PI;