Best JavaScript code snippet using playwright-internal
iterable-async.test.js
Source:iterable-async.test.js
...120 }, 0, arr)[1];121 });122 123 it('should yield accumulated items', async () => {124 await expect(toArray(accumulate(toAsync(pred), iterator)))125 .to.eventually.eql(expected);126 });127 128 it('should work with arrays', async () => {129 await expect(toArray(accumulate(toAsync(pred))(arr)))130 .to.eventually.eql(expected);131 });132 133 it('should be curried', async () => {134 await expect(toArray(accumulate(toAsync(pred))(iterator)))135 .to.eventually.eql(expected);136 });137 138 });139 140 describe('append', () => {141 142 it('should append an element', async () => {143 await expect(toArray(append('test', iterator)))144 .to.eventually.eql(R.append('test', arr));145 });146 147 it('should be curried', async () => {148 await expect(toArray(append('test')(iterator)))149 .to.eventually.eql(R.append('test', arr));150 });151 152 });153 154 describe('concat', () => {155 156 let iterators;157 beforeEach(() => {158 iterators = [159 range(0, 10),160 range(10, 20),161 ];162 expected = R.range(0, 20);163 });164 165 it('should concat iterators', async () => {166 await expect(toArray(concat(...iterators)))167 .to.eventually.eql(expected);168 });169 170 it('should work with arrays', async () => {171 const arrays = await mapP(toArray, iterators);172 await expect(toArray(concat(...arrays)))173 .to.eventually.eql(expected);174 });175 176 it('should be curried', async () => {177 await expect(toArray(concat(iterators[0])(iterators[1])))178 .to.eventually.eql(expected);179 });180 181 });182 183 describe('correspondWith', () => {184 185 let arr1, arr2;186 let iter1, iter2;187 beforeEach(() => {188 arr1 = [189 { id: 123, saved: true },190 { id: 456, saved: false },191 { id: 789, saved: true },192 ];193 arr2 = [194 { id: 123, saved: false },195 { id: 456, saved: true },196 { id: 789, saved: false },197 ];198 iter1 = from(arr1);199 iter2 = from(arr2);200 pred = on(is, R.prop('id'));201 });202 203 it('should return true if elements in same index pass pred', async () => {204 await expect(correspondsWith(toAsync(pred), iter1, iter2))205 .to.eventually.eql(true);206 });207 208 it('should work with arrays', async () => {209 await expect(correspondsWith(toAsync(pred), arr1, arr2))210 .to.eventually.eql(true);211 });212 213 it('should return false if elements in same index do not pass pred', async () => {214 arr1[0].id = 999;215 await expect(correspondsWith(toAsync(pred), iter1, iter2))216 .to.eventually.eql(false);217 });218 219 it('should return false if iterables are different lengths', async () => {220 arr1.pop();221 await expect(correspondsWith(toAsync(pred), iter1, iter2))222 .to.eventually.eql(false);223 });224 225 it('should be curried', async () => {226 await expect(correspondsWith(toAsync(pred))(iter1)(iter2))227 .to.eventually.eql(true);228 });229 230 });231 232 describe('count', () => {233 234 beforeEach(() => {235 pred = n => !!(n % 3);236 });237 238 it('should count number of items for which pred returns true', async () => {239 await expect(count(toAsync(pred), iterator))240 .to.eventually.eql(R.filter(pred, arr).length);241 });242 243 it('should be curried', async () => {244 await expect(count(toAsync(pred))(iterator))245 .to.eventually.eql(R.filter(pred, arr).length);246 });247 248 });249 250 describe('cycle', () => {251 252 let n;253 beforeEach(() => {254 n = random(10, 200);255 expected = R.pipe(256 n => Math.ceil(n / arr.length),257 R.times(() => arr),258 R.flatten,259 R.take(n),260 )(n);261 });262 263 it('should infinitely cycle through the iterator', async () => {264 iterator = R.pipe(cycle, take(n))(iterator);265 await expect(toArray(iterator)).to.eventually.eql(expected);266 });267 268 it('should work with arrays', async () => {269 iterator = R.pipe(cycle, take(n))(arr);270 await expect(toArray(iterator)).to.eventually.eql(expected);271 });272 273 });274 275 describe('cycleN', () => {276 277 let n;278 beforeEach(() => {279 n = 3;280 expected = R.chain(() => arr, [...Array(n)]);281 });282 283 it('should cycle through the iterator n times', async () => {284 iterator = cycleN(n, iterator);285 await expect(toArray(iterator))286 .to.eventually.eql(expected);287 });288 289 it('should yield original items if n = 1', async () => {290 iterator = cycleN(1, iterator);291 await expect(toArray(iterator))292 .to.eventually.eql(arr);293 });294 295 it('should return an empty iterator if n = 0', async () => {296 iterator = cycleN(0, iterator);297 await expect(toArray(iterator))298 .to.eventually.eql([]);299 });300 301 it('should work with arrays', async () => {302 iterator = cycleN(n, arr);303 await expect(toArray(iterator))304 .to.eventually.eql(expected);305 });306 307 it('should be curried', async () => {308 iterator = cycleN(n)(iterator);309 await expect(toArray(iterator))310 .to.eventually.eql(expected);311 });312 313 });314 315 describe('drop', () => {316 317 let n;318 beforeEach(() => {319 n = random(0, 70);320 expected = R.drop(n, arr);321 });322 323 it('should drop n items', async () => {324 await expect(toArray(drop(n, iterator)))325 .to.eventually.eql(expected);326 });327 328 it('should yield all items if n <= 0', async () => {329 await expect(toArray(drop(-1, iterator)))330 .to.eventually.eql(arr);331 });332 333 it('should work with arrays', async () => {334 await expect(toArray(drop(n, arr)))335 .to.eventually.eql(expected);336 });337 338 it('should be curried', async () => {339 await expect(toArray(drop(n)(iterator)))340 .to.eventually.eql(expected);341 });342 343 });344 345 describe('dropLast', () => {346 347 it('should drop the last n items', async () => {348 const n = random(0, arr.length);349 await expect(toArray(dropLast(n, iterator))).to.eventually.eql(350 R.dropLast(n, arr),351 );352 });353 354 it('should be curried', async () => {355 const n = random(0, arr.length);356 await expect(toArray(dropLast(n)(iterator))).to.eventually.eql(357 R.dropLast(n, arr),358 );359 });360 361 });362 363 describe('dropWhile', () => {364 365 beforeEach(() => {366 pred = n => (n < 70 || n > 90);367 expected = R.dropWhile(pred, arr);368 });369 370 it('should drop items while the predicate is satisfied', async () => {371 await expect(toArray(dropWhile(toAsync(pred), iterator)))372 .to.eventually.eql(expected);373 });374 375 it('work with arrays', async () => {376 await expect(toArray(dropWhile(toAsync(pred), arr)))377 .to.eventually.eql(expected);378 });379 380 it('should be curried', async () => {381 await expect(toArray(dropWhile(toAsync(pred))(iterator)))382 .to.eventually.eql(expected);383 });384 385 });386 387 describe('enumerate', () => {388 389 beforeEach(async () => {390 expected = await toArray(zip(391 range(0, arr.length),392 from(arr),393 ));394 });395 396 it('should yield [item, index] tuples', async () => {397 await expect(toArray(enumerate(iterator)))398 .to.eventually.eql(expected);399 });400 401 it('should work with arrays', async () => {402 await expect(toArray(enumerate(arr)))403 .to.eventually.eql(expected);404 });405 406 });407 408 describe('every', () => {409 410 beforeEach(() => {411 pred = (num) => num < 20;412 expected = R.all(pred, arr);413 });414 415 it('should return true if all items pass the predicate', async () => {416 await expect(every(toAsync(pred), iterator))417 .eventually.to.eql(expected);418 });419 420 it('should return false if any item does not pass the predicate', async () => {421 await expect(every(toAsync(R.F), iterator))422 .eventually.to.eql(R.all(R.F, arr));423 });424 425 it('should return true for empty iterators', async () => {426 await expect(every(toAsync(pred), of()))427 .eventually.to.eql(true);428 });429 430 it('should be curried', async () => {431 await expect(every(toAsync(pred))(iterator))432 .eventually.to.eql(expected);433 });434 435 });436 437 describe('exhaust', () => {438 439 it('should return undefined', async () => {440 await expect(exhaust(iterator))441 .to.eventually.eql(undefined);442 });443 444 it('should not exhaust arrays', async () => {445 const items = [...arr];446 await exhaust(arr);447 expect(arr).to.eql(items);448 });449 450 it('should exhaust the iterator', async () => {451 await exhaust(iterator);452 await expect(toArray(iterator))453 .to.eventually.eql([]);454 });455 456 });457 458 describe('filter', () => {459 460 beforeEach(() => {461 pred = num => num % 2;462 expected = R.filter(pred, arr);463 });464 465 it('should yield items that pass the predicate', async () => {466 await expect(toArray(filter(toAsync(pred), iterator)))467 .to.eventually.eql(expected);468 });469 470 it('should work with arrays', async () => {471 await expect(toArray(filter(toAsync(pred), arr)))472 .to.eventually.eql(expected);473 });474 475 it('should be curried', async () => {476 await expect(toArray(filter(toAsync(pred))(iterator)))477 .to.eventually.eql(expected);478 });479 480 });481 482 describe('find', () => {483 484 let toFind;485 beforeEach(() => {486 toFind = sample(arr);487 pred = n => (n === toFind);488 expected = R.find(pred, arr);489 });490 491 it('should return the first item matching predicate', async () => {492 await expect(find(toAsync(pred), iterator))493 .to.eventually.eql(expected);494 });495 496 it('should stop iteration at the first predicate match', async () => {497 const spy = sinon.spy(toAsync(pred));498 await find(spy, iterator);499 expect(spy.callCount)500 .to.eql(R.indexOf(toFind, arr) + 1);501 });502 503 it('should be curried', async () => {504 await expect(find(toAsync(pred))(iterator))505 .to.eventually.eql(expected);506 });507 508 });509 510 describe('flatten', () => {511 512 it('should flatten recursively', async () => {513 arr = [1, [2, 3, [4, 5, 6], [7, 8, 9, [12, 13, 14]]], [9, 10, 11]];514 iterator = from(arr);515 await expect(toArray(flatten(iterator)))516 .to.eventually.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 9, 10, 11]);517 });518 519 });520 521 describe('flattenN', () => {522 523 const input = [1, [2, 3, [4, 5, 6], [7, 8, 9, [12, 13, 14]]], [9, 10, 11]];524 const outputs = {525 0: [1, [2, 3, [4, 5, 6], [7, 8, 9, [12, 13, 14]]], [9, 10, 11]],526 1: [1, 2, 3, [4, 5, 6], [7, 8, 9, [12, 13, 14]], 9, 10, 11],527 2: [1, 2, 3, 4, 5, 6, 7, 8, 9, [12, 13, 14], 9, 10, 11],528 3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 9, 10, 11],529 4: [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 9, 10, 11],530 };531 532 it('should flatten n levels of depth', async () => {533 for (const [n, output] of Object.entries(outputs)) {534 iterator = from(input);535 await expect(toArray(flattenN(+n, iterator)))536 .to.eventually.eql(output);537 }538 });539 540 });541 542 describe('flatIterate', () => {543 544 let num;545 beforeEach(() => {546 num = 10;547 });548 549 it('should yield infinitely', async () => {550 551 iterator = R.applyTo(0, R.pipe(552 flatIterate(function* it(n) {553 yield n - 1;554 yield n;555 yield n + 1;556 557 return n + 1;558 }),559 take(num),560 ));561 562 const expected = R.applyTo(num, R.pipe(563 R.range(0),564 R.chain(n => [n - 1, n, n + 1]),565 R.take(num),566 ));567 568 return expect(toArray(iterator))569 .to.eventually.eql(expected);570 });571 572 });573 574 describe('flatMap', () => {575 576 beforeEach(() => {577 pred = async function* pred(num) {578 while (num--) yield await num;579 };580 arr = R.range(0, 5);581 iterator = from(arr);582 expected = [0, 1, 0, 2, 1, 0, 3, 2, 1, 0];583 });584 585 it('should yield all yielded items from predicate', async () => {586 expect(await toArray(flatMap(pred, iterator)))587 .to.eql(expected);588 });589 590 it('should work with arrays', async () => {591 pred = async (num) => [num, num + 1];592 expect(await toArray(flatMap(pred, arr)))593 .to.eql([0, 1, 1, 2, 2, 3, 3, 4, 4, 5]);594 });595 596 it('should be curried', async () => {597 expect(await toArray(flatMap(pred)(iterator)))598 .to.eql(expected);599 });600 601 });602 603 describe('flatUnfold', () => {604 605 it('should flatten yielded items', async () => {606 iterator = flatUnfold(n => {607 if (n >= 5) return null;608 return [from([n, n * 2]), (n + 1)];609 }, 0);610 await expect(toArray(iterator)).to.eventually.eql([611 0, 0, 1, 2, 2, 4, 3, 6, 4, 8,612 ]);613 });614 615 });616 617 describe('forEach', () => {618 619 it('should call predicate for each yielded item', async () => {620 pred = sinon.stub().resolves();621 await toArray(forEach(pred, iterator));622 const args = pred.args.map(R.head);623 expect(args).to.eql(arr);624 });625 626 it('should yield items from the input iterator', async () => {627 await expect(toArray(forEach(noop, iterator)))628 .to.eventually.eql(arr);629 });630 631 it('should be curried', async () => {632 pred = sinon.stub().resolves();633 await toArray(forEach(pred)(iterator));634 const args = pred.args.map(R.head);635 expect(args).to.eql(arr);636 });637 638 });639 640 describe('frame', () => {641 642 let num;643 beforeEach(() => {644 num = random(1, arr.length - 1);645 expected = R.aperture(num, arr);646 });647 648 it('should yield sliding frames of size n', async () => {649 await expect(toArray(frame(num)(iterator)))650 .to.eventually.eql(expected);651 });652 653 it('should work with arrays', async () => {654 await expect(toArray(frame(num)(arr)))655 .to.eventually.eql(expected);656 });657 658 it('should be curried', async () => {659 await expect(toArray(frame(num)(iterator)))660 .to.eventually.eql(expected);661 });662 663 });664 665 describe('from', () => {666 667 it('should return an async iterator for an iterable', async () => {668 expect(isAsyncIterable(from(arr))).to.eql(true);669 expect(isIterator(from(arr))).to.eql(true);670 expect(await toArray(from(arr))).to.eql(arr);671 });672 673 });674 675 describe('groupWith', () => {676 677 it('should', async () => {678 const preds = [679 (left, right) => (left === right),680 (left, right) => (left !== right),681 (left, right) => (left >= right),682 (left, right) => (left <= right),683 (left, right) => (left + 1 === right),684 (left, right) => (left % 2 === right % 2),685 ];686 687 for (const arr of [688 [0, 1, 1, 2, 2, 3, 4, 5, 5],689 [],690 [0, 0, 0, 0],691 [9, 8, 7, 6, 5, 4, 3, 2, 1],692 [2, 2, 1, 2, 3, 4, 4, 6, 8, 7, 1, 2, 9, 9],693 ]) {694 for (const pred of preds) {695 await expect(toArray(groupWith(toAsync(pred), from(arr))))696 .to.eventually.eql(R.groupWith(pred, arr));697 }698 }699 700 });701 702 });703 704 describe('includes', () => {705 706 let item;707 beforeEach(() => {708 item = sample(arr);709 });710 711 it('should return true if a value in the iterable strictly equals', async () => {712 await expect(includes(item, iterator)).to.eventually.eql(true);713 });714 715 it('should return false if no value in the iterable strictly equals', async () => {716 await expect(includes({ test: true }, iterator)).to.eventually.eql(false);717 });718 719 });720 721 describe('indexOf', () => {722 723 let item;724 beforeEach(() => {725 item = sample(arr);726 });727 728 it('should return the index of the first strictly equal match', async () => {729 await expect(indexOf(item, iterator))730 .to.eventually.eql(R.indexOf(item, arr));731 });732 733 it('should return -1 if no value in the iterable strictly equals', async () => {734 await expect(indexOf({ test: true }, iterator)).to.eventually.eql(-1);735 });736 737 });738 739 describe('indices', () => {740 741 it('should return iterator of indices', async () => {742 await expect(toArray(indices(iterator)))743 .to.eventually.eql(R.keys(arr).map(n => +n));744 });745 746 });747 748 describe('init', () => {749 750 it('should yield every item, but the last', async () => {751 await expect(toArray(init(iterator))).to.eventually.eql(752 R.init(arr)753 );754 });755 756 });757 758 describe('intersperse', () => {759 760 let sep;761 beforeEach(() => {762 sep = '|';763 });764 765 it('should yield seperator between items', async () => {766 await expect(toArray(intersperse(sep, iterator)))767 .to.eventually.eql(R.intersperse(sep, arr));768 });769 770 it('should be curried', async () => {771 await expect(toArray(intersperse(sep)(iterator)))772 .to.eventually.eql(R.intersperse(sep, arr));773 });774 775 });776 777 describe('isEmpty', () => {778 779 it('should return true for empty iterator', async () => {780 await expect(isEmpty(of())).to.eventually.eql(true);781 });782 783 it('should return false for iterators with > 0 items', async () => {784 await expect(isEmpty(of(1))).to.eventually.eql(false);785 });786 787 });788 789 describe('iterate', () => {790 791 let init;792 beforeEach(() => {793 init = 15;794 pred = R.add(10);795 expected = [15, 25, 35, 45, 55];796 });797 798 it('should yield initial item and predicate returns', async () => {799 iterator = take(5, iterate(toAsync(pred), init));800 await expect(toArray(iterator)).to.eventually.eql(expected);801 });802 803 it('should be curried', async () => {804 iterator = take(5, iterate(toAsync(pred))(init));805 await expect(toArray(iterator)).to.eventually.eql(expected);806 });807 808 });809 810 describe('join', () => {811 812 it('should join iterator into string', async () => {813 await expect(join(iterator))814 .to.eventually.eql(R.join('', arr));815 });816 817 });818 819 describe('joinWith', () => {820 821 let sep;822 beforeEach(() => {823 sep = '|';824 });825 826 it('should join iterator into string with interspersed separator', async () => {827 await expect(joinWith(sep, iterator))828 .to.eventually.eql(R.join(sep, arr));829 });830 831 it('should be curried', async () => {832 await expect(joinWith(sep)(iterator))833 .to.eventually.eql(R.join(sep, arr));834 });835 836 });837 838 describe('length', () => {839 840 it('should return the iterator length', async () => {841 await expect(length(iterator)).to.eventually.eql(arr.length);842 });843 844 it('should work with arrays', async () => {845 await expect(length(arr)).to.eventually.eql(arr.length);846 });847 848 it('should exhaust the iterator', async () => {849 await length(iterator);850 await expect(toArray(iterator)).to.eventually.eql([]);851 });852 853 });854 855 describe('map', () => {856 857 beforeEach(async () => {858 pred = R.add(random(10, 20));859 expected = R.map(pred, arr);860 });861 862 it('should transform yielded items with predicate', async () => {863 expect(await toArray(map(toAsync(pred), iterator))).to.eql(expected);864 });865 866 it('should be curried', async () => {867 expect(await toArray(map(toAsync(pred))(iterator))).to.eql(expected);868 });869 870 });871 872 describe('maxBy', () => {873 874 let objs;875 beforeEach(() => {876 objs = arr.map(id => ({ id }));877 iterator = from(objs);878 pred = R.prop('id');879 expected = R.reduce(Math.max, -Infinity, arr);880 });881 882 it('should return max item after pred', async () => {883 await expect(maxBy(toAsync(pred), iterator)).to.eventually.eql(expected);884 });885 886 it('should work on arrays', async () => {887 await expect(maxBy(toAsync(pred), objs)).to.eventually.eql(expected);888 });889 890 it('should be curried', async () => {891 await expect(maxBy(toAsync(pred))(iterator)).to.eventually.eql(expected);892 });893 894 });895 896 describe('minBy', () => {897 898 let objs;899 beforeEach(() => {900 objs = arr.map(id => ({ id }));901 iterator = from(objs);902 pred = R.prop('id');903 expected = R.reduce(Math.min, Infinity, arr);904 });905 906 it('should return max item after pred', async () => {907 await expect(minBy(toAsync(pred), iterator))908 .to.eventually.eql(expected);909 });910 911 it('should work on arrays', async () => {912 await expect(minBy(toAsync(pred), objs))913 .to.eventually.eql(expected);914 });915 916 it('should be curried', async () => {917 await expect(minBy(toAsync(pred))(iterator))918 .to.eventually.eql(expected);919 });920 921 });922 923 describe('next', () => {924 925 it('should return the next value in the iterator', async () => {926 const yields = [927 await next(iterator),928 await next(iterator),929 await next(iterator),930 await next(iterator),931 ];932 expect(yields).to.eql([933 arr[0],934 arr[1],935 arr[2],936 arr[3],937 ]);938 });939 940 it('should return the next value in the iterator', async () => {941 await expect(next(iterator)).to.eventually.eql(R.head(arr));942 });943 944 it('should advance the iterator', async () => {945 await next(iterator);946 await expect(toArray(iterator)).to.eventually.eql(arr.slice(1));947 });948 949 xit('should return ? for arrays', async () => {950 await expect(next(arr)).to.eventually.eql();951 });952 953 it('should throw StopIteration if the iterator is exhausted', async () => {954 await exhaust(iterator);955 await expect(next(iterator))956 .to.eventually.be.rejectedWith(StopIteration);957 });958 959 });960 961 describe('none', () => {962 963 beforeEach(() => {964 pred = n => n < Infinity;965 });966 967 it('should return false if any item passes the predicate', async () => {968 await expect(none(toAsync(pred), iterator)).to.eventually.eql(false);969 });970 971 it('should return true if no items pass the predicate', async () => {972 pred = n => n > Infinity;973 await expect(none(toAsync(pred), iterator)).to.eventually.eql(true);974 });975 976 it('should return true for empty iterators', async () => {977 await expect(none(toAsync(pred), of())).to.eventually.eql(true);978 });979 980 it('should be curried', async () => {981 await expect(none(toAsync(pred))(iterator)).to.eventually.eql(false);982 });983 984 });985 986 describe('nth', () => {987 988 let index;989 beforeEach(() => {990 index = random(0, arr.length - 1);991 expected = R.nth(index, arr);992 });993 994 it('should return the nth item', async () => {995 await expect(nth(index, iterator))996 .to.eventually.eql(expected);997 });998 999 it('should work with arrays', async () => {1000 await expect(nth(index, arr))1001 .to.eventually.eql(expected);1002 });1003 1004 xit('should work with negative indices', async () => {1005 await expect(nth(-index, iterator))1006 .to.eventually.eql(R.nth(-index, arr));1007 });1008 1009 it('should be curried', async () => {1010 await expect(nth(index)(iterator))1011 .to.eventually.eql(expected);1012 });1013 1014 });1015 1016 describe('of', () => {1017 1018 it('should yield arguments', async () => {1019 expect(isAsyncIterable(of(...arr))).to.eql(true);1020 expect(isIterator(of(...arr))).to.eql(true);1021 expect(await toArray(of(...arr))).to.eql(arr);1022 });1023 1024 });1025 1026 describe('pad', () => {1027 1028 let padItem, n;1029 beforeEach(() => {1030 padItem = 'world';1031 n = 100;1032 arr = ['hello', 'this', 'is', 'my'];1033 iterator = pad(padItem, from(arr));1034 expected = [1035 ...arr,1036 ...R.times(R.always(padItem), n - arr.length),1037 ];1038 });1039 1040 it('should pad to infinity', async () => {1041 await expect(toArray(take(n, iterator))).to.eventually.eql(expected);1042 });1043 1044 it('should be curried', async () => {1045 await expect(toArray(take(n)(iterator))).to.eventually.eql(expected);1046 });1047 1048 });1049 1050 describe('padTo', () => {1051 1052 let padItem, n;1053 beforeEach(() => {1054 padItem = 'world';1055 n = random(10, 500);1056 arr = ['hello', 'this', 'is', 'my'];1057 iterator = padTo(n, padItem, from(arr));1058 expected = [1059 ...arr,1060 ...R.times(R.always(padItem), n - arr.length),1061 ];1062 });1063 1064 it('should return iterator of length n', async () => {1065 await expect(length(iterator))1066 .to.eventually.eql(n);1067 });1068 1069 it('should pad to length of n', async () => {1070 await expect(toArray(iterator))1071 .to.eventually.eql(expected);1072 });1073 1074 it('should not truncate when n < iterable length', async () => {1075 iterator = padTo(2, padItem, from(arr));1076 await expect(toArray(iterator))1077 .to.eventually.eql(arr);1078 });1079 1080 it('should be curried', async () => {1081 await expect(toArray(padTo(n)(padItem)(from(arr))))1082 .to.eventually.eql(expected);1083 });1084 1085 });1086 1087 describe('partition', () => {1088 1089 beforeEach(() => {1090 pred = n => n % 2;1091 });1092 1093 it('should bifurcate by pred', async () => {1094 await expect(mapP(toArray, partition(toAsync(pred), iterator)))1095 .to.eventually.eql(R.partition(pred, arr));1096 });1097 1098 it('should be curried', async () => {1099 await expect(mapP(toArray, partition(toAsync(pred))(iterator)))1100 .to.eventually.eql(R.partition(pred, arr));1101 });1102 1103 });1104 1105 describe('prepend', () => {1106 1107 let item;1108 beforeEach(() => {1109 item = 'test';1110 expected = R.prepend(item, arr);1111 });1112 1113 it('should prepend an element', async () => {1114 await expect(toArray(prepend(item, iterator)))1115 .to.eventually.eql(expected);1116 });1117 1118 it('should work with arrays', async () => {1119 await expect(toArray(prepend(item, arr)))1120 .to.eventually.eql(expected);1121 });1122 1123 it('should be curried', async () => {1124 await expect(toArray(prepend(item)(iterator)))1125 .to.eventually.eql(expected);1126 });1127 1128 });1129 1130 describe('range', () => {1131 1132 let iterator;1133 beforeEach(() => {1134 arr = R.range(0, 10);1135 iterator = from(arr);1136 expected = arr;1137 });1138 1139 it('should create an iterator of numbers with inclusive start and exclusive end', async () => {1140 await expect(toArray(iterator))1141 .to.eventually.eql(expected);1142 });1143 1144 it('should work with infinite sequences', async () => {1145 const len = 100;1146 const infinite = range(0, Infinity);1147 const first = take(len, infinite);1148 await expect(toArray(first))1149 .to.eventually.eql(R.range(0, len));1150 });1151 1152 it('should be curried', async () => {1153 await expect(toArray(range(0)(10)))1154 .to.eventually.eql(expected);1155 });1156 1157 });1158 1159 describe('rangeStep', () => {1160 1161 describe('positive step', () => {1162 1163 it('should have an inclusive start and exlusive end', async () => {1164 await expect(toArray(rangeStep(1.25, 15, 30)))1165 .to.eventually.eql([15, 16.25, 17.5, 18.75, 20, 21.25, 22.5, 23.75, 25, 26.25, 27.5, 28.75]);1166 });1167 1168 it('should yield no elements if start > end', async () => {1169 await expect(toArray(rangeStep(1.25, 30, 15)))1170 .to.eventually.eql([]);1171 });1172 1173 });1174 1175 describe('negative step', () => {1176 1177 it('should have an inclusive start and exlusive end', async () => {1178 await expect(toArray(rangeStep(-1.75, 40, 20)))1179 .to.eventually.eql([40, 38.25, 36.5, 34.75, 33, 31.25, 29.5, 27.75, 26, 24.25, 22.5, 20.75]);1180 });1181 1182 it('should yield no elements if start < end', async () => {1183 await expect(toArray(rangeStep(-1.75, 20, 40)))1184 .to.eventually.eql([]);1185 });1186 1187 });1188 1189 it('should return an empty iterable when step = 0', async () => {1190 // asc1191 await expect(toArray(rangeStep(0, 20, 40))).to.eventually.eql([]);1192 // desc1193 await expect(toArray(rangeStep(0, 80, 40))).to.eventually.eql([]);1194 });1195 1196 it('should be curried', async () => {1197 await expect(toArray(rangeStep(5)(15)(30)))1198 .to.eventually.eql([15, 20, 25]);1199 });1200 1201 });1202 1203 describe('reduce', () => {1204 1205 beforeEach(async () => {1206 pred = R.add;1207 expected = await reduceP(pred, 0, arr);1208 });1209 1210 it('should reduce iterator into accumulator', async () => {1211 await expect(reduce(toAsync(pred), 0, iterator))1212 .to.eventually.eql(expected);1213 });1214 1215 it('should work with arrays', async () => {1216 await expect(reduce(toAsync(pred), 0, arr))1217 .to.eventually.eql(expected);1218 });1219 1220 it('should be curried', () => {1221 expect(reduce(toAsync(pred))(0)(iterator))1222 .to.eventually.eql(expected);1223 });1224 1225 });1226 1227 describe('reject', () => {1228 1229 beforeEach(() => {1230 pred = num => num % 2;1231 expected = R.reject(pred, arr);1232 });1233 1234 it('should yield items that do not pass the predicate', async () => {1235 await expect(toArray(reject(toAsync(pred), iterator)))1236 .to.eventually.eql(expected);1237 });1238 1239 });1240 1241 describe('repeat', () => {1242 1243 let n;1244 beforeEach(() => {1245 n = random(1, 20);1246 });1247 1248 it('should repeat the input infinitely', async () => {1249 const thing = { test: true };1250 const taken = toArray(take(n, repeat(thing)));1251 await expect(taken).to.eventually.eql(R.times(() => thing, n));1252 });1253 1254 });1255 1256 describe('reverse', () => {1257 1258 it('should reverse the iterator', async () => {1259 await expect(toArray(reverse(iterator)))1260 .to.eventually.eql(R.reverse(arr));1261 });1262 1263 });1264 1265 describe('scan', () => {1266 1267 beforeEach(() => {1268 pred = R.add;1269 });1270 1271 it('should yield intermediate reductions', async () => {1272 await expect(toArray(scan(toAsync(pred), 1, iterator)))1273 .to.eventually.eql(R.scan(pred, 1, arr));1274 });1275 1276 it('should be curried', async () => {1277 await expect(toArray(scan(toAsync(pred))(1)(iterator)))1278 .to.eventually.eql(R.scan(pred, 1, arr));1279 });1280 1281 });1282 1283 describe('slice', () => {1284 1285 let start, end;1286 beforeEach(() => {1287 const mid = Math.ceil(arr.length / 2);1288 start = random(0, mid);1289 end = random(mid, arr.length);1290 expected = R.slice(start, end, arr);1291 });1292 1293 it('should slice between indexes', async () => {1294 const sliced = slice(start, end, iterator);1295 await expect(toArray(sliced)).to.eventually.eql(expected);1296 });1297 1298 it('should work with arrays', async () => {1299 const sliced = slice(start, end, arr);1300 await expect(toArray(sliced)).to.eventually.eql(expected);1301 });1302 1303 it('should be curried', async () => {1304 const sliced = slice(start)(end)(iterator);1305 await expect(toArray(sliced)).to.eventually.eql(expected);1306 });1307 1308 });1309 1310 describe('some', () => {1311 1312 beforeEach(() => {1313 pred = n => n > 5;1314 expected = R.any(pred, arr);1315 });1316 1317 it('should return true if any item passes the predicate', async () => {1318 await expect(some(toAsync(pred), iterator))1319 .to.eventually.eql(expected);1320 });1321 1322 it('should return false if any item does not pass the predicate', async () => {1323 pred = n => n > Infinity;1324 await expect(some(toAsync(pred), iterator))1325 .to.eventually.eql(R.any(pred, arr));1326 });1327 1328 it('should return false for empty iterators', async () => {1329 await expect(some(toAsync(pred), of()))1330 .to.eventually.eql(false);1331 });1332 1333 it('should be curried', async () => {1334 await expect(some(toAsync(pred))(iterator))1335 .to.eventually.eql(expected);1336 });1337 1338 });1339 1340 describe('splitAt', () => {1341 1342 let n;1343 beforeEach(() => {1344 n = random(0, arr.length - 1);1345 });1346 1347 it('should split iterator at the nth element', async () => {1348 const [left, right] = splitAt(n, iterator);1349 const [leftArr, rightArr] = R.splitAt(n, arr);1350 1351 await expect(toArray(left))1352 .to.eventually.eql(leftArr);1353 1354 await expect(toArray(right))1355 .to.eventually.eql(rightArr);1356 });1357 1358 });1359 1360 describe('splitEvery', () => {1361 1362 let n;1363 beforeEach(() => {1364 n = random(1, 10);1365 });1366 1367 it('should split iterator every n yields', async () => {1368 await expect(toArray(splitEvery(n, iterator)))1369 .to.eventually.eql(R.splitEvery(n, arr));1370 });1371 1372 it('should be curried', async () => {1373 await expect(toArray(splitEvery(n)(iterator)))1374 .to.eventually.eql(R.splitEvery(n, arr));1375 });1376 1377 });1378 1379 describe('sum', () => {1380 1381 beforeEach(() => {1382 expected = R.reduce(R.add, 0, arr);1383 });1384 1385 it('should sum the iterator', async () => {1386 await expect(sum(iterator)).to.eventually.eql(expected);1387 });1388 1389 it('should with with arrays', async () => {1390 await expect(sum(arr)).to.eventually.eql(expected);1391 });1392 1393 });1394 1395 describe('take', () => {1396 1397 let n;1398 beforeEach(() => {1399 n = random(0, arr.length - 1);1400 expected = R.take(n, arr);1401 });1402 1403 it('should yield the first n items', async () => {1404 await expect(toArray(take(n, iterator)))1405 .to.eventually.eql(expected);1406 });1407 1408 it('should yield nothing if n <= 0 ', async () => {1409 await expect(toArray(take(-1, iterator)))1410 .to.eventually.eql([]);1411 });1412 1413 it('should work with arrays', async () => {1414 await expect(toArray(take(n, arr)))1415 .to.eventually.eql(expected);1416 });1417 1418 it('should be curried', async () => {1419 await expect(toArray(take(n)(iterator)))1420 .to.eventually.eql(expected);1421 });1422 1423 });1424 1425 describe('takeWhile', () => {1426 1427 beforeEach(() => {1428 arr = R.range(50, 500);1429 iterator = from(arr);1430 pred = n => (n < 175 || n > 200);1431 expected = R.takeWhile(pred, arr);1432 });1433 1434 it('should take items while pred is true', async () => {1435 await expect(toArray(takeWhile(toAsync(pred), iterator)))1436 .to.eventually.eql(expected);1437 });1438 1439 it('should work with arrays', async () => {1440 await expect(toArray(takeWhile(toAsync(pred), arr)))1441 .to.eventually.eql(expected);1442 });1443 1444 it('should be curried', async () => {1445 await expect(toArray(takeWhile(toAsync(pred))(iterator)))1446 .to.eventually.eql(expected);1447 });1448 1449 });1450 1451 describe('tee', () => {1452 1453 let n, copies;1454 beforeEach(() => {1455 arr = R.range(0, 2);1456 iterator = from(arr);1457 n = 10;1458 copies = tee(n, iterator);1459 expected = [...Array(n)].map(() => arr);1460 });1461 1462 it('should return n copies', async () => {1463 expect(copies.length).to.eql(n);1464 for (const copy of copies) {1465 expect(isAsyncIterable(copy)).to.eql(true);1466 expect(isIterator(copy)).to.eql(true);1467 await expect(toArray(copy))1468 .to.eventually.eql(arr);1469 }1470 });1471 1472 it('should exhaust the input iterator when one copy is exhausted', async () => {1473 tee(n, iterator);1474 await expect(toArray(iterator)).to.eventually.eql(arr);1475 1476 iterator = from(arr);1477 const [copy] = tee(n, iterator);1478 await exhaust(copy);1479 await expect(toArray(iterator)).to.eventually.eql([]);1480 });1481 1482 it('should be curried', async () => {1483 await expect(mapP(toArray, tee(n)(iterator)))1484 .to.eventually.eql(expected);1485 });1486 1487 });1488 1489 describe('times', () => {1490 1491 beforeEach(() => {1492 expected = R.times(R.always('test'), 10);1493 });1494 1495 it('should yield the item n times', async () => {1496 await expect(toArray(times(10, 'test')))1497 .to.eventually.eql(expected);1498 });1499 1500 it('should be curried', async () => {1501 await expect(toArray(times(10)('test')))1502 .to.eventually.eql(expected);1503 });1504 1505 });1506 1507 describe('unique', () => {1508 1509 beforeEach(() => {1510 arr = [1, 2, 3, 3, 3, 2, 1];1511 iterator = from(arr);1512 expected = [1, 2, 3];1513 });1514 1515 it('should yield unique items', async () => {1516 await expect(toArray(unique(iterator)))1517 .to.eventually.eql(expected);1518 });1519 1520 it('should work with arrays', async () => {1521 await expect(toArray(unique(arr)))1522 .to.eventually.eql(expected);1523 });1524 1525 });1526 1527 describe('uniqueWith', () => {1528 1529 beforeEach(() => {1530 arr = [1, 2, 3, 3, 3, 2, 1].map((id) => ({ id }));1531 pred = on(is, R.prop('id'));1532 iterator = from(arr);1533 });1534 1535 it('should yield unique items', async () => {1536 await expect(toArray(uniqueWith(toAsync(pred), iterator)))1537 .to.eventually.eql(R.uniqWith(pred, arr));1538 });1539 1540 it('should work with arrays', async () => {1541 await expect(toArray(uniqueWith(toAsync(pred), arr)))1542 .to.eventually.eql(R.uniqWith(pred, arr));1543 });1544 1545 it('should be curried', async () => {1546 await expect(toArray(uniqueWith(toAsync(pred))(iterator)))1547 .to.eventually.eql(R.uniqWith(pred, arr));1548 });1549 1550 });1551 1552 describe('unfold', () => {1553 1554 beforeEach(() => {1555 pred = n => (n > 50 ? false : [-n, n + 10]);1556 });1557 1558 it('should yield until a falsey value is returned', async () => {1559 await expect(toArray(unfold(toAsync(pred), 10)))1560 .to.eventually.eql(R.unfold(pred, 10));1561 });1562 1563 it('should be curried', async () => {1564 await expect(toArray(unfold(toAsync(pred))(10)))1565 .to.eventually.eql(R.unfold(pred, 10));1566 });1567 1568 });1569 1570 describe('unnest', () => {1571 1572 it('should flatten one level', async () => {1573 arr = [1, [2, 3, [4, 5, 6], [7, 8, 9, [12, 13, 14]]], [9, 10, 11]];1574 iterator = from(arr);1575 await expect(toArray(unnest(iterator)))1576 .to.eventually.eql(R.unnest(arr));1577 });1578 1579 it('should flatten iterators', async () => {1580 arr = [1, from([2, 3, 4, 5])];1581 1582 iterator = from(arr);1583 await expect(toArray(unnest(iterator)))1584 .to.eventually.eql([1, 2, 3, 4, 5]);1585 });1586 1587 });1588 1589 describe('unzip', () => {1590 1591 let arr1, arr2;1592 let range1, range2;1593 beforeEach(() => {1594 arr1 = R.range(0, 10);1595 arr2 = R.range(10, 20);1596 range1 = from(arr1);1597 range2 = from(arr2);1598 });1599 1600 it('should unzip tuples', async () => {1601 const zipped = zip(range1, range2);1602 const [left, right] = unzip(zipped);1603 await expect(toArray(left)).to.eventually.eql(arr1);1604 await expect(toArray(right)).to.eventually.eql(arr2);1605 });1606 1607 });1608 1609 describe('unzipN', () => {1610 1611 let n;1612 beforeEach(() => {1613 n = random(1, 10);1614 arr = [...Array(random(1, 10))]1615 .map(() => [...Array(n)]1616 .map(() => random(1617 random(0, 10),1618 random(10, 50),1619 )));1620 iterator = from(arr);1621 expected = [...Array(n)]1622 .map((_, i) => arr.map(R.nth(i)));1623 });1624 1625 it('should unzip n-pls', async () => {1626 const iterators = unzipN(n, iterator);1627 iterators.forEach((it) => {1628 expect(isAsyncIterable(it)).to.eql(true);1629 expect(isIterator(it)).to.eql(true);1630 });1631 await expect(mapP(toArray, iterators))1632 .to.eventually.eql(expected);1633 });1634 1635 it('should be curried', async () => {1636 await expect(mapP(toArray, unzipN(n)(iterator)))1637 .to.eventually.eql(expected);1638 });1639 1640 });1641 1642 describe('zip', () => {1643 1644 let range1, range2;1645 let iter1, iter2;1646 beforeEach('stub', () => {1647 range1 = R.range(0, 10);1648 range2 = R.range(0, 7).reverse();1649 iter1 = from(range1);1650 iter2 = from(range2);1651 expected = R.zip(range1, range2);1652 });1653 1654 it('should yield pairs until the shortest iterator is exhausted', async () => {1655 await expect(toArray(zip(iter1, iter2)))1656 .to.eventually.eql(expected);1657 });1658 1659 it('should work with arrays', async () => {1660 await expect(toArray(zip(range1, range2)))1661 .to.eventually.eql(expected);1662 });1663 1664 it('should be curried', async () => {1665 await expect(toArray(zip(iter1)(iter2)))1666 .to.eventually.eql(expected);1667 });1668 1669 });1670 1671 describe('zipAllWith', () => {1672 1673 let arrays, iterables, len, n;1674 beforeEach(() => {1675 n = random(1, 10);1676 len = random(1, 10);1677 pred = R.unapply(R.sum);1678 arrays = [...Array(n)].map(() => {1679 const num = random(0, 100);1680 return R.range(num, num + len);1681 });1682 iterables = arrays.map(from);1683 expected = [...Array(len)]1684 .map((_, i) => pred(...arrays.map(R.nth(i))));1685 });1686 1687 it('should zip iterables', async () => {1688 await expect(toArray(zipAllWith(toAsync(pred), iterables)))1689 .to.eventually.eql(expected);1690 });1691 1692 it('should work with arrays', async () => {1693 await expect(toArray(zipAllWith(toAsync(pred), arrays)))1694 .to.eventually.eql(expected);1695 });1696 1697 it('should be curried', async () => {1698 await expect(toArray(zipAllWith(toAsync(pred))(iterables)))1699 .to.eventually.eql(expected);1700 });1701 1702 });1703 1704 describe('zipWith', () => {1705 1706 let range1, range2;1707 let iter1, iter2;1708 beforeEach('stub', () => {1709 pred = R.add;1710 range1 = R.range(0, 10);1711 range2 = R.range(10, 20);1712 iter1 = from(range1);1713 iter2 = from(range2);1714 expected = R.zip(range1, range2);1715 });1716 1717 it('should yield items returned from predicate', async () => {1718 await expect(toArray(zipWith(toAsync(pred), iter1, iter2)))1719 .to.eventually.eql(R.zipWith(pred, range1, range2));1720 });1721 1722 });1723 ...
rx-lite-async.js
Source:rx-lite-async.js
1declare interface Rx$ObservableStatic {2 /**3 * Invokes the asynchronous function, surfacing the result through an observable sequence.4 * @param functionAsync Asynchronous function which returns a Promise to run.5 * @returns An observable sequence exposing the function's result value, or an exception.6 */7 startAsync<T>(functionAsync: () => IPromise<T>): Observable<T>;8 start<T>(func: () => T, context?: any, scheduler?: IScheduler): Observable<T>;9 toAsync<TResult>(10 func: () => TResult,11 context?: any,12 scheduler?: IScheduler13 ): () => Observable<TResult>;14 toAsync<T1, TResult>(15 func: (arg1: T1) => TResult,16 context?: any,17 scheduler?: IScheduler18 ): (arg1: T1) => Observable<TResult>;19 toAsync<T1, TResult>(20 func: (arg1?: T1) => TResult,21 context?: any,22 scheduler?: IScheduler23 ): (arg1?: T1) => Observable<TResult>;24 toAsync<T1, TResult>(25 func: (...args: T1[]) => TResult,26 context?: any,27 scheduler?: IScheduler28 ): (...args: T1[]) => Observable<TResult>;29 toAsync<T1, T2, TResult>(30 func: (arg1: T1, arg2: T2) => TResult,31 context?: any,32 scheduler?: IScheduler33 ): (arg1: T1, arg2: T2) => Observable<TResult>;34 toAsync<T1, T2, TResult>(35 func: (arg1: T1, arg2?: T2) => TResult,36 context?: any,37 scheduler?: IScheduler38 ): (arg1: T1, arg2?: T2) => Observable<TResult>;39 toAsync<T1, T2, TResult>(40 func: (arg1?: T1, arg2?: T2) => TResult,41 context?: any,42 scheduler?: IScheduler43 ): (arg1?: T1, arg2?: T2) => Observable<TResult>;44 toAsync<T1, T2, TResult>(45 func: (arg1: T1, ...args: T2[]) => TResult,46 context?: any,47 scheduler?: IScheduler48 ): (arg1: T1, ...args: T2[]) => Observable<TResult>;49 toAsync<T1, T2, TResult>(50 func: (arg1?: T1, ...args: T2[]) => TResult,51 context?: any,52 scheduler?: IScheduler53 ): (arg1?: T1, ...args: T2[]) => Observable<TResult>;54 toAsync<T1, T2, T3, TResult>(55 func: (arg1: T1, arg2: T2, arg3: T3) => TResult,56 context?: any,57 scheduler?: IScheduler58 ): (arg1: T1, arg2: T2, arg3: T3) => Observable<TResult>;59 toAsync<T1, T2, T3, TResult>(60 func: (arg1: T1, arg2: T2, arg3?: T3) => TResult,61 context?: any,62 scheduler?: IScheduler63 ): (arg1: T1, arg2: T2, arg3?: T3) => Observable<TResult>;64 toAsync<T1, T2, T3, TResult>(65 func: (arg1: T1, arg2?: T2, arg3?: T3) => TResult,66 context?: any,67 scheduler?: IScheduler68 ): (arg1: T1, arg2?: T2, arg3?: T3) => Observable<TResult>;69 toAsync<T1, T2, T3, TResult>(70 func: (arg1?: T1, arg2?: T2, arg3?: T3) => TResult,71 context?: any,72 scheduler?: IScheduler73 ): (arg1?: T1, arg2?: T2, arg3?: T3) => Observable<TResult>;74 toAsync<T1, T2, T3, TResult>(75 func: (arg1: T1, arg2: T2, ...args: T3[]) => TResult,76 context?: any,77 scheduler?: IScheduler78 ): (arg1: T1, arg2: T2, ...args: T3[]) => Observable<TResult>;79 toAsync<T1, T2, T3, TResult>(80 func: (arg1: T1, arg2?: T2, ...args: T3[]) => TResult,81 context?: any,82 scheduler?: IScheduler83 ): (arg1: T1, arg2?: T2, ...args: T3[]) => Observable<TResult>;84 toAsync<T1, T2, T3, TResult>(85 func: (arg1?: T1, arg2?: T2, ...args: T3[]) => TResult,86 context?: any,87 scheduler?: IScheduler88 ): (arg1?: T1, arg2?: T2, ...args: T3[]) => Observable<TResult>;89 toAsync<T1, T2, T3, T4, TResult>(90 func: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult,91 context?: any,92 scheduler?: IScheduler93 ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Observable<TResult>;94 toAsync<T1, T2, T3, T4, TResult>(95 func: (arg1: T1, arg2: T2, arg3: T3, arg4?: T4) => TResult,96 context?: any,97 scheduler?: IScheduler98 ): (arg1: T1, arg2: T2, arg3: T3, arg4?: T4) => Observable<TResult>;99 toAsync<T1, T2, T3, T4, TResult>(100 func: (arg1: T1, arg2: T2, arg3?: T3, arg4?: T4) => TResult,101 context?: any,102 scheduler?: IScheduler103 ): (arg1: T1, arg2: T2, arg3?: T3, arg4?: T4) => Observable<TResult>;104 toAsync<T1, T2, T3, T4, TResult>(105 func: (arg1: T1, arg2?: T2, arg3?: T3, arg4?: T4) => TResult,106 context?: any,107 scheduler?: IScheduler108 ): (arg1: T1, arg2?: T2, arg3?: T3, arg4?: T4) => Observable<TResult>;109 toAsync<T1, T2, T3, T4, TResult>(110 func: (arg1?: T1, arg2?: T2, arg3?: T3, arg4?: T4) => TResult,111 context?: any,112 scheduler?: IScheduler113 ): (arg1?: T1, arg2?: T2, arg3?: T3, arg4?: T4) => Observable<TResult>;114 toAsync<T1, T2, T3, T4, TResult>(115 func: (arg1: T1, arg2: T2, arg3: T3, ...args: T4[]) => TResult,116 context?: any,117 scheduler?: IScheduler118 ): (arg1: T1, arg2: T2, arg3: T3, ...args: T4[]) => Observable<TResult>;119 toAsync<T1, T2, T3, T4, TResult>(120 func: (arg1: T1, arg2: T2, arg3?: T3, ...args: T4[]) => TResult,121 context?: any,122 scheduler?: IScheduler123 ): (arg1: T1, arg2: T2, arg3?: T3, ...args: T4[]) => Observable<TResult>;124 toAsync<T1, T2, T3, T4, TResult>(125 func: (arg1: T1, arg2?: T2, arg3?: T3, ...args: T4[]) => TResult,126 context?: any,127 scheduler?: IScheduler128 ): (arg1: T1, arg2?: T2, arg3?: T3, ...args: T4[]) => Observable<TResult>;129 toAsync<T1, T2, T3, T4, TResult>(130 func: (arg1?: T1, arg2?: T2, arg3?: T3, ...args: T4[]) => TResult,131 context?: any,132 scheduler?: IScheduler133 ): (arg1?: T1, arg2?: T2, arg3?: T3, ...args: T4[]) => Observable<TResult>;134}135declare module "rx-lite-async" {136 declare module.exports: typeof Rx;...
61toasync.js
Source:61toasync.js
...13test('Observable.toAsync context', function (t) {14 var context = { value: 42 };15 var scheduler = new TestScheduler();16 var results = scheduler.startScheduler(function () {17 return Observable.toAsync(function (x) {18 return this.value + x;19 }, context, scheduler)(42);20 });21 reactiveAssert(t, results.messages, [22 onNext(200, 84),23 onCompleted(200)24 ]);25 t.end();26});27test('Observable.toAsync 0', function (t) {28 var scheduler = new TestScheduler();29 var results = scheduler.startScheduler(function () {30 return Observable.toAsync(function () {31 return 0;32 }, null, scheduler)();33 });34 reactiveAssert(t, results.messages, [35 onNext(200, 0),36 onCompleted(200)37 ]);38 t.end();39});40test('Observable.toAsync 1', function (t) {41 var scheduler = new TestScheduler();42 var results = scheduler.startScheduler(function () {43 return Observable.toAsync(function (x) {44 return x;45 }, null, scheduler)(1);46 });47 reactiveAssert(t, results.messages, [48 onNext(200, 1),49 onCompleted(200)50 ]);51 t.end();52});53test('Observable.toAsync 2', function (t) {54 var scheduler = new TestScheduler();55 var results = scheduler.startScheduler(function () {56 return Observable.toAsync(function (x, y) {57 return x + y;58 }, null, scheduler)(1, 2);59 });60 reactiveAssert(t, results.messages, [61 onNext(200, 3),62 onCompleted(200)63 ]);64 t.end();65});66test('Observable.toAsync 3', function (t) {67 var scheduler = new TestScheduler();68 var results = scheduler.startScheduler(function () {69 return Observable.toAsync(function (x, y, z) {70 return x + y + z;71 }, null, scheduler)(1, 2, 3);72 });73 reactiveAssert(t, results.messages, [74 onNext(200, 6),75 onCompleted(200)76 ]);77 t.end();78});79test('Observable.toAsync 4', function (t) {80 var scheduler = new TestScheduler();81 var results = scheduler.startScheduler(function () {82 return Observable.toAsync(function (a, b, c, d) {83 return a + b + c + d;84 }, null, scheduler)(1, 2, 3, 4);85 });86 reactiveAssert(t, results.messages, [87 onNext(200, 10),88 onCompleted(200)89 ]);90 t.end();91});92test('Observable.toAsync error', function (t) {93 var error = new Error();94 var scheduler = new TestScheduler();95 var results = scheduler.startScheduler(function () {96 return Observable.toAsync(function () {97 throw error;98 }, null, scheduler)();99 });100 reactiveAssert(t, results.messages, [101 onError(200, error)102 ]);103 t.end();...
toasync.js
Source:toasync.js
...13test('Observable.toAsync context', function (t) {14 var context = { value: 42 };15 var scheduler = new TestScheduler();16 var results = scheduler.startScheduler(function () {17 return Observable.toAsync(function (x) {18 return this.value + x;19 }, context, scheduler)(42);20 });21 reactiveAssert(t, results.messages, [22 onNext(200, 84),23 onCompleted(200)24 ]);25 t.end();26});27test('Observable.toAsync 0', function (t) {28 var scheduler = new TestScheduler();29 var results = scheduler.startScheduler(function () {30 return Observable.toAsync(function () {31 return 0;32 }, null, scheduler)();33 });34 reactiveAssert(t, results.messages, [35 onNext(200, 0),36 onCompleted(200)37 ]);38 t.end();39});40test('Observable.toAsync 1', function (t) {41 var scheduler = new TestScheduler();42 var results = scheduler.startScheduler(function () {43 return Observable.toAsync(function (x) {44 return x;45 }, null, scheduler)(1);46 });47 reactiveAssert(t, results.messages, [48 onNext(200, 1),49 onCompleted(200)50 ]);51 t.end();52});53test('Observable.toAsync 2', function (t) {54 var scheduler = new TestScheduler();55 var results = scheduler.startScheduler(function () {56 return Observable.toAsync(function (x, y) {57 return x + y;58 }, null, scheduler)(1, 2);59 });60 reactiveAssert(t, results.messages, [61 onNext(200, 3),62 onCompleted(200)63 ]);64 t.end();65});66test('Observable.toAsync 3', function (t) {67 var scheduler = new TestScheduler();68 var results = scheduler.startScheduler(function () {69 return Observable.toAsync(function (x, y, z) {70 return x + y + z;71 }, null, scheduler)(1, 2, 3);72 });73 reactiveAssert(t, results.messages, [74 onNext(200, 6),75 onCompleted(200)76 ]);77 t.end();78});79test('Observable.toAsync 4', function (t) {80 var scheduler = new TestScheduler();81 var results = scheduler.startScheduler(function () {82 return Observable.toAsync(function (a, b, c, d) {83 return a + b + c + d;84 }, null, scheduler)(1, 2, 3, 4);85 });86 reactiveAssert(t, results.messages, [87 onNext(200, 10),88 onCompleted(200)89 ]);90 t.end();91});92test('Observable.toAsync error', function (t) {93 var error = new Error();94 var scheduler = new TestScheduler();95 var results = scheduler.startScheduler(function () {96 return Observable.toAsync(function () {97 throw error;98 }, null, scheduler)();99 });100 reactiveAssert(t, results.messages, [101 onError(200, error)102 ]);103 t.end();...
60toasync.js
Source:60toasync.js
...11 test('toAsync context', function () {12 var context = { value: 42 };13 var scheduler = new TestScheduler();14 var results = scheduler.startScheduler(function () {15 return Observable.toAsync(function (x) {16 return this.value + x;17 }, context, scheduler)(42);18 });19 results.messages.assertEqual(20 onNext(200, 84),21 onCompleted(200)22 );23 });24 test('toAsync 0', function () {25 var scheduler = new TestScheduler();26 var results = scheduler.startScheduler(function () {27 return Observable.toAsync(function () {28 return 0;29 }, null, scheduler)();30 });31 results.messages.assertEqual(32 onNext(200, 0),33 onCompleted(200)34 );35 });36 test('toAsync 1', function () {37 var scheduler = new TestScheduler();38 var results = scheduler.startScheduler(function () {39 return Observable.toAsync(function (x) {40 return x;41 }, null, scheduler)(1);42 });43 results.messages.assertEqual(44 onNext(200, 1),45 onCompleted(200)46 );47 });48 test('toAsync 2', function () {49 var scheduler = new TestScheduler();50 var results = scheduler.startScheduler(function () {51 return Observable.toAsync(function (x, y) {52 return x + y;53 }, null, scheduler)(1, 2);54 });55 results.messages.assertEqual(56 onNext(200, 3),57 onCompleted(200)58 );59 });60 test('toAsync 3', function () {61 var scheduler = new TestScheduler();62 var results = scheduler.startScheduler(function () {63 return Observable.toAsync(function (x, y, z) {64 return x + y + z;65 }, null, scheduler)(1, 2, 3);66 });67 results.messages.assertEqual(68 onNext(200, 6),69 onCompleted(200)70 );71 });72 test('toAsync 4', function () {73 var scheduler = new TestScheduler();74 var results = scheduler.startScheduler(function () {75 return Observable.toAsync(function (a, b, c, d) {76 return a + b + c + d;77 }, null, scheduler)(1, 2, 3, 4);78 });79 results.messages.assertEqual(80 onNext(200, 10),81 onCompleted(200)82 );83 });84 test('toAsync error', function () {85 var error = new Error();86 var scheduler = new TestScheduler();87 var results = scheduler.startScheduler(function () {88 return Observable.toAsync(function () {89 throw error;90 }, null, scheduler)();91 });92 results.messages.assertEqual(93 onError(200, error)94 );95 });...
_index.js
Source:_index.js
...52 if (typeof options == "function") {53 fn = options54 options = {}55 }56 var toAsync = function toAsync() {57 var self = this58 setImmediate(function later() {59 var val = fn()60 if (val === undefined) {61 val = null62 }63 self.push(val)64 })65 }66 return make(options, toAsync)...
sample5.js
Source:sample5.js
...16(() => {17 const st = process.hrtime();18 console.log(`start: ${process.hrtime(st)}`);19 const p = Promise.resolve(1)20 .then(pu.toAsync(inc,10))21 .then(pu.toAsync(inc,10))22 .then(pu.toAsync(inc,10))23 .then(pu.toAsync(inc,10))24 ;25 pu.displayPromise(p.then((v) => {26 console.log(`end: ${process.hrtime(st)}`);27 return v;28 }));29})();30(() => {31 const st = process.hrtime();32 console.log(`start: ${process.hrtime(st)}`);33 const p = Promise.resolve(1)34 .then(pu.toAsync(inc,10))35 .then(pu.toAsync(inc,10))36 .then(pu.toAsync(inc,10))37 .then(pu.toAsync(inc,10))38 ;39 const q = Promise.all([p, p]).then(pu.toAsync(toMulti(add), 100));40 pu.displayPromise(q.then((v) => {41 console.log(`end: ${process.hrtime(st)}`);42 return v;43 }));...
test_rx-lite-async.js
Source:test_rx-lite-async.js
...7 obsNum = Rx.Observable.start(() => 10, obsStr, sch);8 obsNum = Rx.Observable.start(() => 10, obsStr);9 obsNum = Rx.Observable.start(() => 10);10}11function toAsync() {12 obsNum = Rx.Observable.toAsync(() => 1, sch)();13 obsNum = Rx.Observable.toAsync((a1: number) => a1)(1);14 obsStr = <any> Rx.Observable.toAsync((a1: string, a2: number) => a1 + a2.toFixed(0))("", 1);15 obsStr = <any> Rx.Observable.toAsync((a1: string, a2: number, a3: Date) => a1 + a2.toFixed(0) + a3.toDateString())("", 1, new Date());16 obsStr = <any> Rx.Observable.toAsync((a1: string, a2: number, a3: Date, a4: boolean) => a1 + a2.toFixed(0) + a3.toDateString() + (a4 ? 1 : 0))("", 1, new Date(), false);17}18function startAsync() {19 const o: Rx.Observable<string> = Rx.Observable.startAsync(() => <Rx.IPromise<string>> {});...
Using AI Code Generation
1const { toAsync } = require('@playwright/test/lib/utils/async');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const title = await page.title();8 console.log(title);9 await browser.close();10})();11const { toAsync } = require('@playwright/test/lib/utils/async');12const { chromium } = require('playwright');13(async () => {14 const browser = await toAsync(chromium.launch)();15 const context = await toAsync(browser.newContext)();16 const page = await toAsync(context.newPage)();17 const title = await toAsync(page.title)();18 console.log(title);19 await toAsync(browser.close)();20})();21[Apache 2.0](LICENSE)
Using AI Code Generation
1const { toAsync } = require('@playwright/test/lib/utils/async');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 const title = await page.title();7 console.log(title);8 const text = await page.textContent('text=Get started');9 console.log(text);10 const href = await page.getAttribute('css=nav a:nth-of-type(2)', 'href');11 console.log(href);12 await browser.close();13})();14const { toAsync } = require('@playwright/test/lib/utils/async');15const { chromium } = require('playwright');16(async () => {17 const browser = await chromium.launch();18 const page = await browser.newPage();19 const title = await page.title();20 console.log(title);21 const text = await page.textContent('text=Get started');22 console.log(text);23 const href = await page.getAttribute('css=nav a:nth-of-type(2)', 'href');24 console.log(href);25 await browser.close();26})();27const { toAsync } = require('@playwright/test/lib/utils/async');28const { chromium } = require('playwright');29(async () => {30 const browser = await chromium.launch();31 const page = await browser.newPage();32 const title = await page.title();33 console.log(title);34 const text = await page.textContent('text=Get started');35 console.log(text);36 const href = await page.getAttribute('css=nav a
Using AI Code Generation
1const { toAsync } = require('playwright/lib/utils/utils');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 await toAsync(page.waitForSelector)('text=Get started');7 await browser.close();8})();9import { toAsync } from 'playwright/lib/utils/utils';10import { chromium } from 'playwright';11(async () => {12 const browser = await chromium.launch();13 const page = await browser.newPage();14 await toAsync(page.waitForSelector)('text=Get started');15 await browser.close();16})();
Using AI Code Generation
1const { toAsync } = require('playwright-core/lib/utils/async');2const { chromium } = require('playwright-core');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 const asyncPage = toAsync(page);7 console.log(await asyncPage.title());8 await browser.close();9})();
Using AI Code Generation
1const { toAsync } = require('playwright/lib/utils/async');2const { chromium } = require('playwright');3const { toMatchImageSnapshot } = require('jest-image-snapshot');4expect.extend({ toMatchImageSnapshot });5(async () => {6 const browser = await chromium.launch();7 const page = await browser.newPage();8 await page.setViewportSize({ width: 1280, height: 800 });9 await page.screenshot({ path: 'example.png' });10 expect(await page.screenshot()).toMatchImageSnapshot();11 await browser.close();12})();13module.exports = {14};15{16 "scripts": {17 },18 "devDependencies": {19 }20}21 expect(received).toMatchImageSnapshot(snapshotIdentifier)
Using AI Code Generation
1const { toAsync } = require('playwright/lib/utils/async');2const fs = require('fs');3const path = require('path');4const { chromium } = require('playwright');5const { expect } = require('@playwright/test');6const { test, expect } = require('@playwright/test');7const { describe, it } = require('@playwright/test');8const { beforeAll, afterAll } = require('@playwright/test');9const { beforeEach, afterEach } = require('@playwright/test');10const { test, expect } = require('@playwright/test');11const { describe, it } = require('@playwright/test');12const { beforeAll, afterAll } = require('@playwright/test');13const { beforeEach, afterEach } = require('@playwright/test');14const { chromium } = require('playwright');15const fs = require('fs');16const path = require('path');17const { toAsync } = require('playwright/lib/utils/async');18const { test, expect } = require('@playwright/test');19const { describe, it } = require('@playwright/test');20const { beforeAll, afterAll } = require('@playwright/test');21const { beforeEach, afterEach } = require('@playwright/test');22const { chromium } = require('playwright');23const fs = require('fs');24const path = require('path');25const { toAsync } = require('playwright/lib/utils/async');26const { test, expect } = require('@playwright/test');27const { describe, it } = require('@playwright/test');28const { beforeAll, afterAll } = require('@playwright/test');29const { beforeEach, afterEach } = require('@playwright/test');30const { chromium } = require('playwright');31const fs = require('fs');32const path = require('path');33const { toAsync } = require('playwright/lib/utils/async');34const { toAsync } = require('playwright/lib/utils/async');35const fs = require('fs');36const path = require('path');37const { toAsync } = require('playwright/lib/utils/async');38const fs = require('fs');39const path = require('path');40const { toAsync } = require('playwright/lib
Using AI Code Generation
1const { toAsync } = require("playwright-core/lib/utils/utils");2const { chromium } = require("playwright-core");3const browser = await chromium.launch();4const page = await browser.newPage();5const myFunc = toAsync(async (a, b, c) => {6 return a + b + c;7});8const result = await myFunc(1, 2, 3);9console.log(result);10await browser.close();
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!