UNPKG

2.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3require("../polyfill-array");
4const type_1 = require("../type");
5var maps;
6(function (maps) {
7 function array(xs, f) {
8 return xs.map(f);
9 }
10 maps.array = array;
11 function object(x, f) {
12 const y = new x.constructor();
13 /* tslint:disable:forin */
14 for (const k in x) {
15 y[k] = f(x[k], k, x);
16 }
17 /* tslint:enable:forin */
18 return y;
19 }
20 maps.object = object;
21 function set(xs, f) {
22 const ys = new Set();
23 xs.forEach(x => ys.add(f(x, xs)));
24 return ys;
25 }
26 maps.set = set;
27 function map(xs, valueMapper, keyMapper) {
28 const ys = new Map();
29 xs.forEach((av, ak) => {
30 const bv = valueMapper(av, ak, xs);
31 const bk = keyMapper ? keyMapper(av, ak, xs) : ak;
32 ys.set(bk, bv);
33 });
34 return ys;
35 }
36 maps.map = map;
37 function nodeList(xs, f) {
38 const ys = new Array(xs.length);
39 for (let i = 0; i < xs.length; i++) {
40 ys[i] = f(xs.item(i), i, xs);
41 }
42 return ys;
43 }
44 maps.nodeList = nodeList;
45 function any(o, f) {
46 if (Array.isArray(o)) {
47 return array(o, f);
48 }
49 // skip this data type if in node.js
50 if ((typeof NodeList !== 'undefined' && o instanceof NodeList) ||
51 (typeof HTMLCollection !== 'undefined' && o instanceof HTMLCollection)) {
52 return nodeList(o, f);
53 }
54 switch (type_1.getObjectType(o)) {
55 case 'Array':
56 return array(o, f);
57 case 'Set':
58 return set(o, f);
59 case 'Map':
60 return map(o, f);
61 case 'Object':
62 return object(o, f);
63 default:
64 return f(o);
65 }
66 }
67 maps.any = any;
68})(maps = exports.maps || (exports.maps = {}));
69exports.map_array = maps.array;
70exports.map_object = maps.object;
71exports.map_set = maps.set;
72exports.map_map = maps.map;
73exports.map_nodeList = maps.nodeList;
74exports.map_any = maps.any;
75//# sourceMappingURL=map.js.map
\No newline at end of file