-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
1448628
commit dc0b649
Showing
7 changed files
with
57 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
'use strict'; | ||
|
||
module.exports = iterable => { | ||
export default function magicIterable(iterable) { | ||
return new Proxy(iterable, { | ||
get(target, property) { | ||
return function (...args) { | ||
const ret = []; | ||
return function (...arguments_) { | ||
const returnValue = []; | ||
|
||
let i = 0; | ||
let index = 0; | ||
for (const item of target) { | ||
i++; | ||
index++; | ||
|
||
if (typeof item[property] === 'undefined') { | ||
throw new TypeError(`Item ${i} of the iterable is missing the ${property}() method`); | ||
throw new TypeError(`Item ${index} of the iterable is missing the ${property}() method`); | ||
} | ||
|
||
ret.push(Reflect.apply(item[property], item, args)); | ||
returnValue.push(Reflect.apply(item[property], item, arguments_)); | ||
} | ||
|
||
return ret; | ||
return returnValue; | ||
}; | ||
} | ||
}, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,83 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
import magicIterable from './index.js'; | ||
|
||
test('main', t => { | ||
let i = 0; | ||
|
||
const createFixture = () => { | ||
return { | ||
increment(value) { | ||
i += value; | ||
return i; | ||
} | ||
}; | ||
}; | ||
let index = 0; | ||
|
||
const createFixture = () => ({ | ||
increment(value) { | ||
index += value; | ||
return index; | ||
}, | ||
}); | ||
|
||
const array = [ | ||
createFixture(), | ||
createFixture(), | ||
createFixture(), | ||
createFixture() | ||
createFixture(), | ||
]; | ||
|
||
const magicArray = m(array); | ||
const magicArray = magicIterable(array); | ||
|
||
t.true(Array.isArray(magicArray)); | ||
t.deepEqual(magicArray.increment(2), [2, 4, 6, 8]); | ||
t.is(i, 8); | ||
t.is(index, 8); | ||
}); | ||
|
||
test('`this` works correctly', t => { | ||
const fixture = { | ||
i: 0, | ||
index: 0, | ||
increment(value) { | ||
this.i += value; | ||
return this.i; | ||
} | ||
this.index += value; | ||
return this.index; | ||
}, | ||
}; | ||
|
||
const array = [fixture, fixture, fixture, fixture]; | ||
|
||
t.deepEqual(m(array).increment(2), [2, 4, 6, 8]); | ||
t.is(fixture.i, 8); | ||
t.deepEqual(magicIterable(array).increment(2), [2, 4, 6, 8]); | ||
t.is(fixture.index, 8); | ||
}); | ||
|
||
test('does not work on heterogeneous iterable', t => { | ||
const createFixture = () => { | ||
return { | ||
foo() {} | ||
}; | ||
}; | ||
const createFixture = () => ({ | ||
foo() {}, | ||
}); | ||
|
||
const array = [ | ||
createFixture(), | ||
createFixture(), | ||
{}, | ||
createFixture() | ||
createFixture(), | ||
]; | ||
|
||
const magicArray = m(array); | ||
const magicArray = magicIterable(array); | ||
|
||
t.throws(() => { | ||
magicArray.foo(); | ||
}, /Item 3 of the iterable is missing the foo\(\) method/); | ||
}, { | ||
message: /Item 3 of the iterable is missing the foo\(\) method/, | ||
}); | ||
}); | ||
|
||
test('should work on array of non-objects', t => { | ||
t.deepEqual(m(['a', 'b']).includes('b'), [false, true]); | ||
t.deepEqual(magicIterable(['a', 'b']).includes('b'), [false, true]); | ||
}); | ||
|
||
test('should only apply to the items of the iterable', t => { | ||
const fixture = { | ||
foo() { | ||
return '🦄'; | ||
} | ||
}, | ||
}; | ||
|
||
const array = [fixture, fixture]; | ||
array.foo = () => '🤡'; | ||
|
||
t.deepEqual(m(array).foo(), ['🦄', '🦄']); | ||
t.deepEqual(magicIterable(array).foo(), ['🦄', '🦄']); | ||
}); | ||
|
||
test.failing('should support properties, not just methods', t => { | ||
t.deepEqual(m(['a', 'ab', 'abc']).length.toString(), ['1', '2', '3']); | ||
t.deepEqual(magicIterable(['a', 'ab', 'abc']).length.toString(), ['1', '2', '3']); | ||
}); |