Skip to content

Instantly share code, notes, and snippets.

@otwm
Last active January 27, 2021 04:16
Show Gist options
  • Save otwm/3a6358e53ca794cc2e57ade4af91d3bb to your computer and use it in GitHub Desktop.
Save otwm/3a6358e53ca794cc2e57ade4af91d3bb to your computer and use it in GitHub Desktop.
ramda - argument null change
// infra code
const isEmptyOrNil = (value) => isEmpty(value) || isNil(value)
const nullChange = (defaultValue) => (value) => isEmptyOrNil(value)? defaultValue: value
// single null
const _somef = (value) => value
const somefWithDefault = (defaultValue) => pipe(nullChange(defaultValue), _somef)
const somef = somefWithDefault('myValue')
somef(null)
// many args
const _somef2 = (value, value2, value3) => value + value2 + value3
const nullChangeMyValue = nullChange('myValue')
const somef2 = useWith(_somef2, [nullChangeMyValue, nullChangeMyValue, nullChangeMyValue])
somef2(null, undefined, 1234)
// many args2 version2
const nullChangeWith = (defaultValue) => nullChange(defaultValue)
const arrayNullChangeWith = (defaultValue) => (array) => array.map(nullChangeWith(defaultValue))
const arg2Array = (...args) => args
const funcApplyDefaultValue = (defaultValue) => (func) => pipe(
arg2Array, arrayNullChangeWith(defaultValue), apply(func)
)
const v2somef2 = funcApplyDefaultValue(8)(_somef2)
v2somef2(1,2,null)
@otwm
Copy link
Author

otwm commented Jan 26, 2021

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