Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a function for returning the params after executed #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions lib/returnValue.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import * as R from 'ramda'

const returnValue = R.curry(
(paramIndice, fn, ...args) => {
// const fnArgs = []
// if (args.length > 0) {
//
// return returnValue(paramIndice, fn
// }
import {
isFunction
} from './utilities/helper.js'

console.log(args)

const fnArgs = args
const returnValueHelper = (fn, targetParams, params) => (...args) => {
const allParams = [...params, ...args]
const result = R.when(isFunction, R.apply(R.__, args))(fn)
return isFunction(result) ? returnValueHelper(result, targetParams, allParams)
: targetParams.length === 0 ? result
: allParams.filter((p, i) => targetParams.includes(i))
}

console.log(fn(...fnArgs))
// TODO: FP

// fn(...fnArgs)
// const resultIsFunction = ({ result }) => isFunction(result)
//
// const returnValueFunction = ({ result, targetParams, params }) => (...args) => returnValueHelper2(R.apply(result, args), targetParams, [...params, ...args])
//
// const hasTargetParams = ({ targetParams }) => targetParams.length > 0
//
// const getTargetParams = ({ targetParams, params }) => params.filter((p, i) => targetParams.includes(i))
//
// const returnValueHelper2 = (result, targetParams, params) => R.ifElse(
// resultIsFunction,
// returnValueFunction,
// R.ifElse(
// hasTargetParams,
// getTargetParams,
// R.prop('result')
// )
// )({ result, targetParams, params })

return fnArgs[paramIndice]
}
)
const returnValue = (fn, targetParams = []) => R.when(
isFunction,
R.curry(returnValueHelper)(R.__, targetParams, [])
)(fn)

export {
returnValue
}
//
// const test = (a1, a2) => a1
// const foo = 'bar'
// const hello = 'world'
5 changes: 4 additions & 1 deletion lib/utilities/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const getFn = R.curry(
)(fnPath)
)

const isFunction = fn => fn && {}.toString.call(fn) === '[object Function]'

export {
pathLens,
getFn
getFn,
isFunction
}
48 changes: 35 additions & 13 deletions test/returnValue.spec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
const {
returnValue,
append
returnValue
} = require('../index.js')
const chai = require('chai')
const expect = chai.expect
chai.use(require('chai-dom'))
const R = require('ramda')

describe('replaceWith()', function () {
let element, returnedValue, returnedValue2
describe('returnValue()', function () {
const doSomething = (...args) => args
const fn = (a, b, c, d) => doSomething(a, b, c, d)
const cFn = a => (b, c, ...args1) => (d, ...args2) => doSomething(a, b, c, ...args1, d, ...args2)
const a = { A: 1 }
const b = 2
const c = [3]
const d = 4
const e = '5'
const f = 6
const g = true

beforeEach(function () {
require('basichtml').init()
it('should return parameters of a curried function based on parameters indices', function () {
expect(returnValue(fn, [1, 3])(a, b, c, d)).to.have.deep.ordered.members([b, d])
expect(returnValue(R.curry(fn), [0, 3, 4])(a)(b, c, d)).to.have.deep.ordered.members([a, d])
expect(returnValue(R.curry(fn), [0, 1, 2, 3])(a)(b, c)(d)).to.have.deep.ordered.members([a, b, c, d])
})

it('should return result of the curried function if no indices are specified', function () {
expect(returnValue(fn)(a, b, c, d)).to.have.deep.ordered.members(fn(a, b, c, d))
expect(returnValue(fn, [])(a, b, c, d)).to.have.deep.ordered.members(fn(a, b, c, d))
expect(returnValue(R.curry(fn))(a)(b)(c)(d)).to.have.deep.ordered.members(fn(a, b, c, d))
})

element = document.createElement('p')
it('should return parameters of a manually curried function based on parameters indices', function () {
expect(returnValue(cFn, [1, 3])(a)(b, c, d)(e, f, g)).to.have.deep.ordered.members([b, d])
expect(returnValue(cFn, [0, 3, 4, 5, 7])(a)(b, c)(d, e, f, g)).to.have.deep.ordered.members([a, d, e, f])
expect(returnValue(cFn, [0, 1, 2, 3, 4, 5, 6])(a)(b, c, d, e, f)(g)).to.have.deep.ordered.members([a, b, c, d, e, f, g])
})

// returnedValue = returnValue(0, append)(element)(document.body)
// returnedValue2 = returnValue(0, append)(element, document.body)
returnedValue2 = returnValue(0, append)
it('should return result of the manually curried function if no indices are specified', function () {
expect(returnValue(cFn)(a)(b, c, d)(e, f, g)).to.have.deep.ordered.members(cFn(a)(b, c, d)(e, f, g))
expect(returnValue(cFn, [])(a)(b, c, d)(e, f, g)).to.have.deep.ordered.members(cFn(a)(b, c, d)(e, f, g))
expect(returnValue(cFn)(a)(b, c, d, e)(f, g)).to.have.deep.ordered.members(cFn(a)(b, c, d, e)(f, g))
})

it('should return parameter of a function based on parameter number', function () {
// expect(returnedValue).to.equal(element)
// expect(returnedValue2).to.equal(element)
it('should return very first parameter if it is not a function', function () {
expect(returnValue(a)).to.eql(a)
expect(returnValue(a, [0, 2])).to.eql(a)
})
})