Skip to content

Commit

Permalink
add index right
Browse files Browse the repository at this point in the history
  • Loading branch information
Deyan Totev committed Jul 16, 2023
1 parent df64eac commit 72365d7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 1 addition & 2 deletions NEXT_VERSION_CHECKLIST.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
- addIndex
- addIndexRight

---
- addIndex
- addIndexRight
- ap
- aperture
- applyTo
Expand Down
16 changes: 16 additions & 0 deletions source/addIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ export function addIndex(originalFunction){
return originalFunction.apply(this, args)
})
}
export function addIndexRight(originalFunction){
return curryN(originalFunction.length, function (){
const origFn = arguments[ 0 ]
const list = arguments[ arguments.length - 1 ]
let idx = list.length - 1
const args = Array.prototype.slice.call(arguments, 0)
args[ 0 ] = function (){
const result = origFn.apply(this, _concat(arguments, [ idx, list ]))
idx -= 1

return result
}

return originalFunction.apply(this, args)
})
}
9 changes: 8 additions & 1 deletion source/addIndex.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addIndex } from './addIndex.js'
import { addIndex, addIndexRight } from './addIndex.js'
import { map } from './map.js'

test('happy', () => {
Expand All @@ -21,6 +21,13 @@ test('happy', () => {
}
const result = mapIndexed(fn2, [ 1, 2, 3 ])
expect(result).toEqual([ 6, 8, 10 ])

const revmap = (fn, ary) => map(fn, ary)
const revmapIndexed = addIndexRight(revmap)
// [ '5-f', '4-o', '3-o', '2-b', '1-a', '0-r' ]
const a = revmapIndexed((val, idx) => idx + '-' + val,
[ 'f', 'o', 'o', 'b', 'a', 'r' ])
console.log(a)
})

describe('unary functions like `map`', () => {
Expand Down

0 comments on commit 72365d7

Please sign in to comment.