umt 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/module/Function/curry.d.ts +141 -0
- package/module/Function/curry.js +11 -0
- package/module/Function/curry.js.map +1 -0
- package/module/Function/index.d.ts +1 -0
- package/module/Function/index.js +2 -0
- package/module/Function/index.js.map +1 -0
- package/module/Tool/index.d.ts +1 -0
- package/module/Tool/index.js +1 -0
- package/module/Tool/index.js.map +1 -1
- package/module/Tool/pipe.d.ts +66 -0
- package/module/Tool/pipe.js +98 -0
- package/module/Tool/pipe.js.map +1 -0
- package/module/Validate/index.d.ts +1 -0
- package/module/Validate/index.js +1 -0
- package/module/Validate/index.js.map +1 -1
- package/module/Validate/isBun.js +0 -1
- package/module/Validate/isBun.js.map +1 -1
- package/module/Validate/isString.d.ts +9 -0
- package/module/Validate/isString.js +12 -0
- package/module/Validate/isString.js.map +1 -0
- package/module/es5/Function/curry.d.ts +141 -0
- package/module/es5/Function/curry.js +38 -0
- package/module/es5/Function/index.d.ts +1 -0
- package/module/es5/Function/index.js +16 -0
- package/module/es5/Tool/index.d.ts +1 -0
- package/module/es5/Tool/index.js +11 -0
- package/module/es5/Tool/pipe.d.ts +66 -0
- package/module/es5/Tool/pipe.js +129 -0
- package/module/es5/Validate/index.d.ts +1 -0
- package/module/es5/Validate/index.js +11 -0
- package/module/es5/Validate/isBun.js +0 -1
- package/module/es5/Validate/isString.d.ts +9 -0
- package/module/es5/Validate/isString.js +16 -0
- package/module/es5/index.d.ts +1 -0
- package/module/es5/index.js +11 -0
- package/module/es5/tests/unit/Function/curry.test.d.ts +1 -0
- package/module/es5/tests/unit/Function/curry.test.js +138 -0
- package/module/es5/tests/unit/Tool/pipe.test.d.ts +1 -0
- package/module/es5/tests/unit/Tool/pipe.test.js +445 -0
- package/module/es5/tsconfig.tsbuildinfo +1 -1
- package/module/index.d.ts +1 -0
- package/module/index.js +1 -0
- package/module/index.js.map +1 -1
- package/package.json +19 -18
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
var _pipe = require("@/Tool/pipe");
|
|
5
|
+
var _Validate = require("@/Validate");
|
|
6
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
8
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
9
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
10
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
11
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
12
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
13
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
14
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
15
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
16
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
17
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
18
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
19
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
20
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
21
|
+
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
|
22
|
+
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
|
23
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
24
|
+
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
|
25
|
+
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
|
26
|
+
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
27
|
+
describe("Pipe", function () {
|
|
28
|
+
// Direct constructor test
|
|
29
|
+
it("creates an instance with the constructor", function () {
|
|
30
|
+
var pipeLine = new _pipe.Pipe(42);
|
|
31
|
+
expect(pipeLine).toBeInstanceOf(_pipe.Pipe);
|
|
32
|
+
expect(pipeLine.end()).toBe(42);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Extension test
|
|
36
|
+
it("can be extended with additional methods", function () {
|
|
37
|
+
// Extended pipe class with additional functionality
|
|
38
|
+
var ExtendedPipe = /*#__PURE__*/function (_Pipe) {
|
|
39
|
+
function ExtendedPipe() {
|
|
40
|
+
_classCallCheck(this, ExtendedPipe);
|
|
41
|
+
return _callSuper(this, ExtendedPipe, arguments);
|
|
42
|
+
}
|
|
43
|
+
_inherits(ExtendedPipe, _Pipe);
|
|
44
|
+
return _createClass(ExtendedPipe, [{
|
|
45
|
+
key: "double",
|
|
46
|
+
value: function _double() {
|
|
47
|
+
var currentValue = this.end();
|
|
48
|
+
if (typeof currentValue === "number") {
|
|
49
|
+
return new ExtendedPipe(currentValue * 2);
|
|
50
|
+
}
|
|
51
|
+
throw new Error("Value is not a number");
|
|
52
|
+
}
|
|
53
|
+
}, {
|
|
54
|
+
key: "prefix",
|
|
55
|
+
value: function prefix(str) {
|
|
56
|
+
return new ExtendedPipe("".concat(str).concat(this.end()));
|
|
57
|
+
}
|
|
58
|
+
}]);
|
|
59
|
+
}(_pipe.Pipe); // Helper function for the extended pipe
|
|
60
|
+
function extendedPipe(value) {
|
|
61
|
+
return new ExtendedPipe(value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Test the extended functionality
|
|
65
|
+
var result = extendedPipe(5)["double"]().map(function (x) {
|
|
66
|
+
return x + 1;
|
|
67
|
+
}).end();
|
|
68
|
+
expect(result).toBe(11);
|
|
69
|
+
var strResult = extendedPipe("world").prefix("hello ").map(function (s) {
|
|
70
|
+
return s.toUpperCase();
|
|
71
|
+
}).end();
|
|
72
|
+
expect(strResult).toBe("HELLO WORLD");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Basic functionality tests
|
|
76
|
+
it("returns initial value when calling end()", function () {
|
|
77
|
+
var result = (0, _pipe.pipe)(1).end();
|
|
78
|
+
expect(result).toBe(1);
|
|
79
|
+
});
|
|
80
|
+
it("transforms value with map()", function () {
|
|
81
|
+
var result = (0, _pipe.pipe)(1).map(function (x) {
|
|
82
|
+
return x + 1;
|
|
83
|
+
}).end();
|
|
84
|
+
expect(result).toBe(2);
|
|
85
|
+
});
|
|
86
|
+
it("chains multiple map() operations", function () {
|
|
87
|
+
var result = (0, _pipe.pipe)(1).map(function (x) {
|
|
88
|
+
return x + 1;
|
|
89
|
+
}).map(function (x) {
|
|
90
|
+
return x * 2;
|
|
91
|
+
}).map(function (x) {
|
|
92
|
+
return x - 1;
|
|
93
|
+
}).end();
|
|
94
|
+
expect(result).toBe(3);
|
|
95
|
+
});
|
|
96
|
+
it("applies transformation when condition is true in when()", function () {
|
|
97
|
+
var result = (0, _pipe.pipe)(5).when(function (x) {
|
|
98
|
+
return x > 3;
|
|
99
|
+
}, function (x) {
|
|
100
|
+
return x * 2;
|
|
101
|
+
}).end();
|
|
102
|
+
expect(result).toBe(10);
|
|
103
|
+
});
|
|
104
|
+
it("skips transformation when condition is false in when()", function () {
|
|
105
|
+
var result = (0, _pipe.pipe)(2).when(function (x) {
|
|
106
|
+
return x > 3;
|
|
107
|
+
}, function (x) {
|
|
108
|
+
return x * 2;
|
|
109
|
+
}).end();
|
|
110
|
+
expect(result).toBe(2);
|
|
111
|
+
});
|
|
112
|
+
it("executes side effect and preserves original value with tap()", function () {
|
|
113
|
+
var sideEffect = 0;
|
|
114
|
+
var result = (0, _pipe.pipe)(5).tap(function (x) {
|
|
115
|
+
sideEffect = x;
|
|
116
|
+
}).end();
|
|
117
|
+
expect(result).toBe(5);
|
|
118
|
+
expect(sideEffect).toBe(5);
|
|
119
|
+
});
|
|
120
|
+
it("correctly infers types", function () {
|
|
121
|
+
var numberPipe = (0, _pipe.pipe)(1);
|
|
122
|
+
var stringPipe = numberPipe.map(function (x) {
|
|
123
|
+
return "".concat(x);
|
|
124
|
+
});
|
|
125
|
+
var result = stringPipe.end();
|
|
126
|
+
expect(result).toBe("1");
|
|
127
|
+
});
|
|
128
|
+
it("handles complex type inference correctly", function () {
|
|
129
|
+
var user = {
|
|
130
|
+
id: 1,
|
|
131
|
+
name: "John Doe",
|
|
132
|
+
age: 30
|
|
133
|
+
};
|
|
134
|
+
var post = (0, _pipe.pipe)(user).map(function (user) {
|
|
135
|
+
return {
|
|
136
|
+
id: 1,
|
|
137
|
+
title: "First Post",
|
|
138
|
+
content: "Hello, world!",
|
|
139
|
+
author: user
|
|
140
|
+
};
|
|
141
|
+
}).end();
|
|
142
|
+
expect(post).toEqual({
|
|
143
|
+
id: 1,
|
|
144
|
+
title: "First Post",
|
|
145
|
+
content: "Hello, world!",
|
|
146
|
+
author: {
|
|
147
|
+
id: 1,
|
|
148
|
+
name: "John Doe",
|
|
149
|
+
age: 30
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
it("correctly infers nested generic types", function () {
|
|
154
|
+
var users = (0, _pipe.pipe)([]).map(function (users) {
|
|
155
|
+
return [].concat(_toConsumableArray(users), [{
|
|
156
|
+
id: 1,
|
|
157
|
+
name: "John Doe",
|
|
158
|
+
age: 30
|
|
159
|
+
}]);
|
|
160
|
+
}).map(function (users) {
|
|
161
|
+
return [].concat(_toConsumableArray(users), [{
|
|
162
|
+
id: 2,
|
|
163
|
+
name: "Jane Smith",
|
|
164
|
+
age: 25
|
|
165
|
+
}]);
|
|
166
|
+
}).end();
|
|
167
|
+
expect(users).toEqual([{
|
|
168
|
+
id: 1,
|
|
169
|
+
name: "John Doe",
|
|
170
|
+
age: 30
|
|
171
|
+
}, {
|
|
172
|
+
id: 2,
|
|
173
|
+
name: "Jane Smith",
|
|
174
|
+
age: 25
|
|
175
|
+
}]);
|
|
176
|
+
});
|
|
177
|
+
it("processes null as initial value correctly", function () {
|
|
178
|
+
var result = (0, _pipe.pipe)(null).end();
|
|
179
|
+
expect(result).toBeNull();
|
|
180
|
+
});
|
|
181
|
+
it("processes undefined as initial value correctly", function () {
|
|
182
|
+
var result = (0, _pipe.pipe)(undefined).end();
|
|
183
|
+
expect(result).toBeUndefined();
|
|
184
|
+
});
|
|
185
|
+
it("processes empty strings correctly", function () {
|
|
186
|
+
var result = (0, _pipe.pipe)("").map(function (x) {
|
|
187
|
+
return x + "test";
|
|
188
|
+
}).map(function (x) {
|
|
189
|
+
return x.toUpperCase();
|
|
190
|
+
}).end();
|
|
191
|
+
expect(result).toBe("TEST");
|
|
192
|
+
});
|
|
193
|
+
it("processes numeric zero correctly", function () {
|
|
194
|
+
var result = (0, _pipe.pipe)(0).map(function (x) {
|
|
195
|
+
return x + 1;
|
|
196
|
+
}).map(function (x) {
|
|
197
|
+
return x * 2;
|
|
198
|
+
}).end();
|
|
199
|
+
expect(result).toBe(2);
|
|
200
|
+
});
|
|
201
|
+
it("processes boolean transformations correctly", function () {
|
|
202
|
+
var result = (0, _pipe.pipe)(true).map(function (x) {
|
|
203
|
+
return !x;
|
|
204
|
+
}).map(function (x) {
|
|
205
|
+
return x.toString();
|
|
206
|
+
}).end();
|
|
207
|
+
expect(result).toBe("false");
|
|
208
|
+
});
|
|
209
|
+
it("processes array transformations correctly", function () {
|
|
210
|
+
var result = (0, _pipe.pipe)([1, 2, 3, 4, 5]).map(function (arr) {
|
|
211
|
+
return arr.filter(function (x) {
|
|
212
|
+
return x % 2 === 0;
|
|
213
|
+
});
|
|
214
|
+
}).map(function (arr) {
|
|
215
|
+
return arr.map(function (x) {
|
|
216
|
+
return x * 2;
|
|
217
|
+
});
|
|
218
|
+
}).end();
|
|
219
|
+
expect(result).toEqual([4, 8]);
|
|
220
|
+
});
|
|
221
|
+
it("processes complex object transformations correctly", function () {
|
|
222
|
+
var initial = {
|
|
223
|
+
count: 0,
|
|
224
|
+
items: []
|
|
225
|
+
};
|
|
226
|
+
var result = (0, _pipe.pipe)(initial).map(function (data) {
|
|
227
|
+
return _objectSpread(_objectSpread({}, data), {}, {
|
|
228
|
+
count: data.count + 1
|
|
229
|
+
});
|
|
230
|
+
}).map(function (data) {
|
|
231
|
+
return _objectSpread(_objectSpread({}, data), {}, {
|
|
232
|
+
items: [].concat(_toConsumableArray(data.items), ["Item ".concat(data.count)])
|
|
233
|
+
});
|
|
234
|
+
}).end();
|
|
235
|
+
expect(result).toEqual({
|
|
236
|
+
count: 1,
|
|
237
|
+
items: ["Item 1"]
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
it("processes multiple type transformations in chain", function () {
|
|
241
|
+
var result = (0, _pipe.pipe)(123).map(function (x) {
|
|
242
|
+
return x.toString();
|
|
243
|
+
}).map(function (x) {
|
|
244
|
+
return x.split("");
|
|
245
|
+
}).map(function (arr) {
|
|
246
|
+
return arr.map(Number);
|
|
247
|
+
}).end();
|
|
248
|
+
expect(result).toEqual([1, 2, 3]);
|
|
249
|
+
});
|
|
250
|
+
it("combines when() and map() for complex processing", function () {
|
|
251
|
+
var result = (0, _pipe.pipe)(5).map(function (x) {
|
|
252
|
+
return x + 2;
|
|
253
|
+
}).when(function (x) {
|
|
254
|
+
return x > 5;
|
|
255
|
+
}, function (x) {
|
|
256
|
+
return x * 2;
|
|
257
|
+
}).when(function (x) {
|
|
258
|
+
return x < 5;
|
|
259
|
+
}, function (x) {
|
|
260
|
+
return x - 1;
|
|
261
|
+
}).map(function (x) {
|
|
262
|
+
return x + 1;
|
|
263
|
+
}).end();
|
|
264
|
+
expect(result).toBe(15);
|
|
265
|
+
});
|
|
266
|
+
describe("filterStrict()", function () {
|
|
267
|
+
function isValidUser(user) {
|
|
268
|
+
return "verified" in user;
|
|
269
|
+
}
|
|
270
|
+
it("filters and narrows type using type predicate", function () {
|
|
271
|
+
var result = (0, _pipe.pipe)(42).filterStrict(function (x) {
|
|
272
|
+
return (0, _Validate.isNumber)(x, false);
|
|
273
|
+
}).map(function (x) {
|
|
274
|
+
return x + 1;
|
|
275
|
+
}).end();
|
|
276
|
+
expect(result).toBe(43);
|
|
277
|
+
});
|
|
278
|
+
it("throws error when filter condition is not met", function () {
|
|
279
|
+
expect(function () {
|
|
280
|
+
(0, _pipe.pipe)("not a number").filterStrict(_Validate.isNumber).end();
|
|
281
|
+
}).toThrow("Value did not match filter predicate");
|
|
282
|
+
});
|
|
283
|
+
it("filters complex types with interface predicates", function () {
|
|
284
|
+
var user = {
|
|
285
|
+
id: 1,
|
|
286
|
+
name: "John",
|
|
287
|
+
age: 30,
|
|
288
|
+
verified: true
|
|
289
|
+
};
|
|
290
|
+
var result = (0, _pipe.pipe)(user).filterStrict(isValidUser).map(function (u) {
|
|
291
|
+
return u.verified;
|
|
292
|
+
}).end();
|
|
293
|
+
expect(result).toBe(true);
|
|
294
|
+
});
|
|
295
|
+
it("combines filterStrict with when and map operations", function () {
|
|
296
|
+
var result = (0, _pipe.pipe)(5).filterStrict(function (x) {
|
|
297
|
+
return (0, _Validate.isNumber)(x, false);
|
|
298
|
+
}).map(function (x) {
|
|
299
|
+
return x + 2;
|
|
300
|
+
}).when(function (x) {
|
|
301
|
+
return x > 5;
|
|
302
|
+
}, function (x) {
|
|
303
|
+
return x * 2;
|
|
304
|
+
}).end();
|
|
305
|
+
expect(result).toBe(14);
|
|
306
|
+
});
|
|
307
|
+
it("correctly handles type narrowing with string | number union types", function () {
|
|
308
|
+
var stringResult = (0, _pipe.pipe)("hello").filterStrict(_Validate.isString).map(function (x) {
|
|
309
|
+
return x + " world";
|
|
310
|
+
}).end();
|
|
311
|
+
expect(stringResult).toBe("hello world");
|
|
312
|
+
var numberResult = (0, _pipe.pipe)(123).filterStrict(function (x) {
|
|
313
|
+
return (0, _Validate.isNumber)(x, false);
|
|
314
|
+
}).map(function (x) {
|
|
315
|
+
return x * 2;
|
|
316
|
+
}).end();
|
|
317
|
+
expect(numberResult).toBe(246);
|
|
318
|
+
var whenResult = (0, _pipe.pipe)(5).filterStrict(function (x) {
|
|
319
|
+
return (0, _Validate.isNumber)(x, false);
|
|
320
|
+
}).when(function (x) {
|
|
321
|
+
return x > 0;
|
|
322
|
+
}, function (x) {
|
|
323
|
+
return x * 2;
|
|
324
|
+
}).end();
|
|
325
|
+
expect(whenResult).toBe(10);
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
describe("filterWithDefault()", function () {
|
|
329
|
+
it("returns original value when predicate is true", function () {
|
|
330
|
+
var result = (0, _pipe.pipe)(42).filterWithDefault(function (x) {
|
|
331
|
+
return (0, _Validate.isNumber)(x, false);
|
|
332
|
+
}, 0).map(function (x) {
|
|
333
|
+
return x + 1;
|
|
334
|
+
}).end();
|
|
335
|
+
expect(result).toBe(43);
|
|
336
|
+
});
|
|
337
|
+
it("returns default value when predicate is false", function () {
|
|
338
|
+
var result = (0, _pipe.pipe)("not a number").filterWithDefault(function (x) {
|
|
339
|
+
return (0, _Validate.isNumber)(x, false);
|
|
340
|
+
}, 0).map(function (x) {
|
|
341
|
+
return x + 1;
|
|
342
|
+
}).end();
|
|
343
|
+
expect(result).toBe(1);
|
|
344
|
+
});
|
|
345
|
+
it("works with complex types", function () {
|
|
346
|
+
function isValidUser(user) {
|
|
347
|
+
return "verified" in user;
|
|
348
|
+
}
|
|
349
|
+
var invalidUser = {
|
|
350
|
+
id: 1,
|
|
351
|
+
name: "John",
|
|
352
|
+
age: 30
|
|
353
|
+
};
|
|
354
|
+
var defaultUser = {
|
|
355
|
+
id: 0,
|
|
356
|
+
name: "Default",
|
|
357
|
+
age: 0,
|
|
358
|
+
verified: false
|
|
359
|
+
};
|
|
360
|
+
var result = (0, _pipe.pipe)(invalidUser).filterWithDefault(isValidUser, defaultUser).map(function (u) {
|
|
361
|
+
return u.verified;
|
|
362
|
+
}).end();
|
|
363
|
+
expect(result).toBe(false);
|
|
364
|
+
});
|
|
365
|
+
it("maintains type narrowing in subsequent operations", function () {
|
|
366
|
+
var result = (0, _pipe.pipe)("hello").filterWithDefault(function (x) {
|
|
367
|
+
return (0, _Validate.isNumber)(x, false);
|
|
368
|
+
}, 5).map(function (x) {
|
|
369
|
+
return x * 2;
|
|
370
|
+
}) // x is definitely a number here
|
|
371
|
+
.end();
|
|
372
|
+
expect(result).toBe(10);
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
describe("filterResult()", function () {
|
|
376
|
+
it("returns success result when predicate is true", function () {
|
|
377
|
+
var result = (0, _pipe.pipe)(42).filterResult(function (x) {
|
|
378
|
+
return (0, _Validate.isNumber)(x, false);
|
|
379
|
+
}).map(function (result) {
|
|
380
|
+
if (result.type === "success") {
|
|
381
|
+
return result.value + 1;
|
|
382
|
+
}
|
|
383
|
+
return 0;
|
|
384
|
+
}).end();
|
|
385
|
+
expect(result).toBe(43);
|
|
386
|
+
});
|
|
387
|
+
it("returns error result when predicate is false", function () {
|
|
388
|
+
var result = (0, _pipe.pipe)("not a number").filterResult(function (x) {
|
|
389
|
+
return (0, _Validate.isNumber)(x, false);
|
|
390
|
+
}).map(function (result) {
|
|
391
|
+
if (result.type === "error") {
|
|
392
|
+
return result.error.message;
|
|
393
|
+
}
|
|
394
|
+
return "";
|
|
395
|
+
}).end();
|
|
396
|
+
expect(result).toBe("Value did not match filter predicate");
|
|
397
|
+
});
|
|
398
|
+
it("can be used with complex types", function () {
|
|
399
|
+
function isValidUser(user) {
|
|
400
|
+
return "verified" in user;
|
|
401
|
+
}
|
|
402
|
+
var validUser = {
|
|
403
|
+
id: 1,
|
|
404
|
+
name: "John",
|
|
405
|
+
age: 30,
|
|
406
|
+
verified: true
|
|
407
|
+
};
|
|
408
|
+
var result = (0, _pipe.pipe)(validUser).filterResult(isValidUser).map(function (result) {
|
|
409
|
+
if (result.type === "success") {
|
|
410
|
+
return result.value.verified;
|
|
411
|
+
}
|
|
412
|
+
return false;
|
|
413
|
+
}).end();
|
|
414
|
+
expect(result).toBe(true);
|
|
415
|
+
});
|
|
416
|
+
it("can be chained with other operations", function () {
|
|
417
|
+
var result = (0, _pipe.pipe)(5).filterResult(function (x) {
|
|
418
|
+
return (0, _Validate.isNumber)(x, false);
|
|
419
|
+
}).map(function (result) {
|
|
420
|
+
if (result.type === "success") {
|
|
421
|
+
return result.value * 2;
|
|
422
|
+
}
|
|
423
|
+
return 0;
|
|
424
|
+
}).map(function (x) {
|
|
425
|
+
return x + 1;
|
|
426
|
+
}).end();
|
|
427
|
+
expect(result).toBe(11);
|
|
428
|
+
});
|
|
429
|
+
it("handles error cases gracefully", function () {
|
|
430
|
+
var processValue = function processValue(value) {
|
|
431
|
+
return (0, _pipe.pipe)(value).filterResult(function (x) {
|
|
432
|
+
return (0, _Validate.isNumber)(x, false);
|
|
433
|
+
}).map(function (result) {
|
|
434
|
+
if (result.type === "success") {
|
|
435
|
+
return result.value * 2;
|
|
436
|
+
}
|
|
437
|
+
return 0; // Default value for error case
|
|
438
|
+
}).end();
|
|
439
|
+
};
|
|
440
|
+
expect(processValue(10)).toBe(20);
|
|
441
|
+
expect(processValue("not a number")).toBe(0);
|
|
442
|
+
expect(processValue(null)).toBe(0);
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
});
|