shell-ahk
Version:
`Lodash`/`jQuery` for `AHK`.
37 lines (27 loc) • 809 B
text/coffeescript
# @ts-check
import $getType from './getType'
import $length from './length'
import $push from './push'
import $reverse from './reverse'
###* @type import('../type/module').Slice ###
export default (list, start = 0, end = 0) ->
unless $getType list
throw "$.slice: invalid type '#{$getType list}'"
$len = $length list
unless end then end = $len
if start > $len then start = $len
if start < 0 then start = $len + start
if end > $len then end = $len
if end < 0 then end = $len + end
$isReverse = false
if start > end
$isReverse = true
[start, end] = [end, start]
$listNew = []
$n = 0
$max = end - start
while $n < $max
$push $listNew, list[start + $n]
$n++
if $isReverse then return $reverse $listNew
return $listNew