Skip to content

Instantly share code, notes, and snippets.

@ovcharik
Last active November 30, 2017 18:36
Show Gist options
  • Save ovcharik/6b663fe41f2eea0106d20e55a2fb0577 to your computer and use it in GitHub Desktop.
Save ovcharik/6b663fe41f2eea0106d20e55a2fb0577 to your computer and use it in GitHub Desktop.
gen = (cur, all, chunk = 10) ->
half = (chunk - 4) // 2
ledge = Math.max(1 , cur - half)
redge = Math.min(all, cur + half)
ldiff = half - (cur - ledge) + ((ledge < half-1) && 2 || (ledge < half) && 1 || 0)
rdiff = half - (redge - cur) + ((cur+half >= all) && 2 || (cur+half+1 == all) && 1 || 0)
prev = 0
return [1, ...[ledge-rdiff .. redge+ldiff], all]
.filter (v) -> 0 < v <= all
.reduce ((m, v) -> if m.indexOf(v) >= 0 then m else [...m, v]), []
.reduce ((m, v) ->
m.push('..') if prev and v - prev > 2
m.push(prev + 1) if prev and v - prev == 2
m.push(v)
prev = v
return m
), []
generatePage = (cur, all = 35, chunkSize = 10) ->
chunk = Math.ceil cur / chunkSize
ledge = (chunk - 1) * chunkSize + 1
ledge = all - chunkSize if all - ledge < chunkSize
redge = chunk * chunkSize
prev = 0
return [1, ...[ledge .. redge], all]
.filter (v) -> 0 < v <= all
.reduce ((m, v) -> if m.indexOf(v) >= 0 then m else [...m, v]), []
.reduce ((result, p) ->
result.push '..' if prev and p - prev > 2
result.push prev + 1 if prev and p - prev == 2
result.push p
prev = p
return result
), []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment