redioactive
Version:
Reactive streams for chaining overlapping promises.
66 lines (65 loc) • 2.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const redio_1 = __importStar(require("../redio"));
describe('Mapping values in a stream', () => {
test('Convert some uppercase strings to lowercase', async () => {
await expect((0, redio_1.default)(['ONE', 'Two', 'ThReE'])
.map((x) => x.toLowerCase())
.toArray()).resolves.toEqual(['one', 'two', 'three']);
});
test('Convert strings to numbers', async () => {
await expect((0, redio_1.default)(['1', '-2.3', '42'])
.map((x) => +x)
.toArray()).resolves.toEqual([1, -2.3, 42]);
});
test('Copes with an empty streem', async () => {
await expect((0, redio_1.default)([])
.map((x) => +x)
.toArray()).resolves.toEqual([]);
});
test('Mapping via a promise', async () => {
await expect((0, redio_1.default)(['1', '2', '3'])
.map((x) => new Promise((resolve) => {
setTimeout(() => resolve(+x), 1);
}))
.toArray()).resolves.toEqual([1, 2, 3]);
});
test('One to many mapping - double up', async () => {
await expect((0, redio_1.default)(['1', '2', '3'])
.map((x) => [+x, +x], { oneToMany: true })
.toArray()).resolves.toEqual([1, 1, 2, 2, 3, 3]);
});
test('Many to less mapping - select evens', async () => {
await expect((0, redio_1.default)([1, 2, 3, 4, 5, 6])
.map((x) => (x % 2 === 0 ? [x] : []), { oneToMany: true })
.toArray()).resolves.toEqual([2, 4, 6]);
});
test('One to many with end', async () => {
await expect((0, redio_1.default)([1, 2, 3, 4, 5, 6])
.map((x) => (x < 4 ? [x] : [4, redio_1.end]), { oneToMany: true })
.toArray()).resolves.toEqual([1, 2, 3, 4]);
});
});