Created
January 7, 2019 20:33
-
-
Save u8sand/39892f1ac91920b55f9246076c64debd to your computer and use it in GitHub Desktop.
A pythonic `range` function for javascript
This file contains hidden or 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
export function range(start, end) { | |
if (end === undefined) { | |
end = start | |
start = 0 | |
} | |
function *_range() { | |
for(let i = start; i < end; i++) { | |
yield i | |
} | |
} | |
return [..._range()] | |
} |
This file contains hidden or 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
import assert from 'assert' | |
import { range } from './range' | |
describe('range', () => { | |
it('works with 1 arg', () => { | |
assert.deepEqual( | |
range(2), | |
[ | |
0, 1 | |
] | |
) | |
}) | |
it('works with 2 args', () => { | |
assert.deepEqual( | |
range(2, 6), | |
[ | |
2, 3, 4, 5, | |
] | |
) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment