Skip to content

Instantly share code, notes, and snippets.

@u8sand
Created January 7, 2019 20:33
Show Gist options
  • Save u8sand/39892f1ac91920b55f9246076c64debd to your computer and use it in GitHub Desktop.
Save u8sand/39892f1ac91920b55f9246076c64debd to your computer and use it in GitHub Desktop.
A pythonic `range` function for javascript
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()]
}
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