Skip to content

Instantly share code, notes, and snippets.

@kevinpeno
Last active June 26, 2017 18:44
Show Gist options
  • Save kevinpeno/99f27f1af90f41bc79a4d076d371dddf to your computer and use it in GitHub Desktop.
Save kevinpeno/99f27f1af90f41bc79a4d076d371dddf to your computer and use it in GitHub Desktop.
Hope you've had your coffee.

Hope you've had your coffee. I'm trying to figure out why this works:

state.map((good) => {
	if ( good.type === type ) {
		return _.assign(good, { amount })
	}
	else {
		return good
	}
})

but this doesn't:

_.map(state, _.cond([
	[
		_.matchesProperty("type", type),
		_.partialRight(_.assign, { amount })
	],
	[_.stubTrue, _.identity]
]))

Assuming the following:

const state = [{ type: "thing", amount: 0 }]
const type = "thing"
const amount = 1

The output of both should be:

[{ type: "thing", amount: 1 }]

however the output to the second option is:

[{ type: "thing", amount: 0 }]
@kevinpeno
Copy link
Author

kevinpeno commented Jun 26, 2017

Full failing test:

/* globals require, console */
"use strict"

const _ = require("lodash")
const assign = _.partial(_.assign, {})
const state = [{ "type": "thing", "amount": 0 }]
const type = "thing"
const amount = 1

const results = _.map(state, _.cond([
	[
		_.matchesProperty("type", type),
		_.partialRight(assign, { amount })
	],
	[_.stubTrue, _.identity]
]))

console.log(results)
// [ { type: 'thing', amount: 0 } ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment