|
1 | | -import * as ObjectUtil from '../../src/functions.js'; |
| 1 | +import * as ObjectUtil from '../../src/functions'; |
2 | 2 |
|
3 | 3 | const s_OBJECT_DEEP = |
4 | 4 | { |
@@ -149,14 +149,14 @@ describe('ObjectUtil:', () => |
149 | 149 | assert.isTrue(Object.isFrozen(testObj.level1.level2.array2[2][0])); |
150 | 150 |
|
151 | 151 | // Make sure pushing to arrays fails. |
152 | | - // @ts-expect-error |
| 152 | + // @ts-expect-error |
153 | 153 | assert.throws(() => { testObj.a.a1.push(1); }); // @ts-expect-error |
154 | 154 | assert.throws(() => { testObj.c.push(1); }); // @ts-expect-error |
155 | 155 | assert.throws(() => { testObj.array.push(1); }); // @ts-expect-error |
156 | 156 | assert.throws(() => { testObj.array[0].push(1); }); // @ts-expect-error |
157 | 157 | assert.throws(() => { testObj.array[1].push(1); }); // @ts-expect-error |
158 | 158 | assert.throws(() => { testObj.array[2].push(1); }); |
159 | | - // @ts-expect-error |
| 159 | + // @ts-expect-error |
160 | 160 | assert.throws(() => { testObj.level1.d.d1.push(1); }); // @ts-expect-error |
161 | 161 | assert.throws(() => { testObj.level1.f.push(1); }); // @ts-expect-error |
162 | 162 | assert.throws(() => { testObj.level1.array1.push(1); }); // @ts-expect-error |
@@ -430,14 +430,14 @@ describe('ObjectUtil:', () => |
430 | 430 | assert.isTrue(Object.isSealed(testObj.level1.level2.array2[2][0])); |
431 | 431 |
|
432 | 432 | // Make sure pushing to arrays fails. |
433 | | - // @ts-expect-error |
| 433 | + // @ts-expect-error |
434 | 434 | assert.throws(() => { testObj.a.a1.push(1); }); // @ts-expect-error |
435 | 435 | assert.throws(() => { testObj.c.push(1); }); // @ts-expect-error |
436 | 436 | assert.throws(() => { testObj.array.push(1); }); // @ts-expect-error |
437 | 437 | assert.throws(() => { testObj.array[0].push(1); }); // @ts-expect-error |
438 | 438 | assert.throws(() => { testObj.array[1].push(1); }); // @ts-expect-error |
439 | 439 | assert.throws(() => { testObj.array[2].push(1); }); |
440 | | - // @ts-expect-error |
| 440 | + // @ts-expect-error |
441 | 441 | assert.throws(() => { testObj.level1.d.d1.push(1); }); // @ts-expect-error |
442 | 442 | assert.throws(() => { testObj.level1.f.push(1); }); // @ts-expect-error |
443 | 443 | assert.throws(() => { testObj.level1.array1.push(1); }); // @ts-expect-error |
@@ -467,6 +467,55 @@ describe('ObjectUtil:', () => |
467 | 467 | assert.isTrue(Object.isSealed(testObj.level1.level2.skipKey.s3)); |
468 | 468 | }); |
469 | 469 |
|
| 470 | + |
| 471 | + it('ensureNonEmptyAsyncIterable:', async () => |
| 472 | + { |
| 473 | + // @ts-expect-error |
| 474 | + assert.isUndefined(await ObjectUtil.ensureNonEmptyAsyncIterable(false)); |
| 475 | + |
| 476 | + assert.isUndefined(await ObjectUtil.ensureNonEmptyAsyncIterable((async function *generator() {})())); |
| 477 | + assert.isUndefined(await ObjectUtil.ensureNonEmptyAsyncIterable(null)); |
| 478 | + assert.isUndefined(await ObjectUtil.ensureNonEmptyAsyncIterable(void 0)); |
| 479 | + assert.isUndefined(await ObjectUtil.ensureNonEmptyAsyncIterable([])); |
| 480 | + assert.isUndefined(await ObjectUtil.ensureNonEmptyAsyncIterable((function *generator() {})())); |
| 481 | + |
| 482 | + const asyncIter1 = await ObjectUtil.ensureNonEmptyAsyncIterable( |
| 483 | + (async function *generator() { yield 1; yield 2; })()); |
| 484 | + |
| 485 | + const asyncIter2 = await ObjectUtil.ensureNonEmptyAsyncIterable((function *generator() { yield 1; yield 2; })()); |
| 486 | + |
| 487 | + const asyncIter3 = await ObjectUtil.ensureNonEmptyAsyncIterable([1, 2]); |
| 488 | + |
| 489 | + const result1 = []; |
| 490 | + const result2 = []; |
| 491 | + const result3 = []; |
| 492 | + |
| 493 | + for await (const v of asyncIter1) { result1.push(v); } |
| 494 | + for await (const v of asyncIter2) { result2.push(v); } |
| 495 | + for await (const v of asyncIter3) { result3.push(v); } |
| 496 | + |
| 497 | + assert.deepEqual(result1, [1, 2]); |
| 498 | + assert.deepEqual(result2, [1, 2]); |
| 499 | + assert.deepEqual(result3, [1, 2]); |
| 500 | + }); |
| 501 | + |
| 502 | + it('ensureNonEmptyIterable:', () => |
| 503 | + { |
| 504 | + // @ts-expect-error |
| 505 | + assert.isUndefined(ObjectUtil.ensureNonEmptyIterable(false)); |
| 506 | + |
| 507 | + // @ts-expect-error |
| 508 | + assert.isUndefined(ObjectUtil.ensureNonEmptyIterable((async function *generator() {})())); |
| 509 | + |
| 510 | + assert.isUndefined(ObjectUtil.ensureNonEmptyIterable(null)); |
| 511 | + assert.isUndefined(ObjectUtil.ensureNonEmptyIterable(void 0)); |
| 512 | + assert.isUndefined(ObjectUtil.ensureNonEmptyIterable([])); |
| 513 | + assert.isUndefined(ObjectUtil.ensureNonEmptyIterable((function *generator() {})())); |
| 514 | + |
| 515 | + assert.deepEqual([...ObjectUtil.ensureNonEmptyIterable((function *generator() { yield 1; yield 2; })())], [1, 2]); |
| 516 | + assert.deepEqual([...ObjectUtil.ensureNonEmptyIterable([1, 2])], [1, 2]); |
| 517 | + }); |
| 518 | + |
470 | 519 | describe('hasAccessor:', () => |
471 | 520 | { |
472 | 521 | it('top level', () => |
@@ -602,9 +651,9 @@ describe('ObjectUtil:', () => |
602 | 651 | assert.isFalse(ObjectUtil.isIterable(false)); |
603 | 652 | assert.isFalse(ObjectUtil.isIterable(null)); |
604 | 653 | assert.isFalse(ObjectUtil.isIterable({})); |
605 | | - assert.isFalse(ObjectUtil.isIterable('')); |
606 | 654 | assert.isFalse(ObjectUtil.isIterable((async function *generator() {})())); |
607 | 655 |
|
| 656 | + assert.isTrue(ObjectUtil.isIterable('123')); |
608 | 657 | assert.isTrue(ObjectUtil.isIterable(new Set('a'))); |
609 | 658 | assert.isTrue(ObjectUtil.isIterable((function *generator() {})())); |
610 | 659 | }); |
@@ -645,6 +694,22 @@ describe('ObjectUtil:', () => |
645 | 694 | assert.isTrue(ObjectUtil.isPlainObject(new Object())); // eslint-disable-line no-new-object |
646 | 695 | }); |
647 | 696 |
|
| 697 | + it('isPlainObjectEmpty', () => |
| 698 | + { |
| 699 | + class Test {} |
| 700 | + |
| 701 | + assert.isFalse(ObjectUtil.isPlainObjectEmpty(false)); |
| 702 | + assert.isFalse(ObjectUtil.isPlainObjectEmpty(null)); |
| 703 | + assert.isFalse(ObjectUtil.isPlainObjectEmpty(void 0)); |
| 704 | + assert.isFalse(ObjectUtil.isPlainObjectEmpty(new String('test'))); |
| 705 | + assert.isFalse(ObjectUtil.isPlainObjectEmpty(new Test())); |
| 706 | + assert.isFalse(ObjectUtil.isPlainObjectEmpty({ foo: 'bar ' })); |
| 707 | + |
| 708 | + assert.isTrue(ObjectUtil.isPlainObjectEmpty({})); |
| 709 | + assert.isTrue(ObjectUtil.isPlainObjectEmpty(Object.create(null))); |
| 710 | + assert.isTrue(ObjectUtil.isPlainObjectEmpty(new Object())); // eslint-disable-line no-new-object |
| 711 | + }); |
| 712 | + |
648 | 713 | it('objectKeys', () => |
649 | 714 | { |
650 | 715 | // @ts-expect-error |
|
0 commit comments