Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-deps   js

Repository URL to install this package:

Version: 6.0.4 

/ src / fp / propOr.ts

import { AnyArrayOrObj } from '../_typings'
import { has } from '../is'
import curry from './curry'

/**
 * If the given, non-null object has an own property with the specified name,
 * returns the value of that property. Otherwise returns the provided default
 * value.
 * @since 5.0.0-beta.9
 *
 * @memberOf fp
 * @fork v0.6.0
 * @category Object
 * @sig a -> String -> Object -> a
 * @param {*} val The default value.
 * @param {String} p The name of the property to return.
 * @param {Object} obj The object to query.
 * @return {*} The value of given property of the supplied object or the default value.
 * @example
 *
 *      var alice = {
 *        name: 'ALICE',
 *        age: 101
 *      };
 *      var favorite = prop('favoriteLibrary');
 *      var favoriteWithDefault = propOr('Ramda', 'favoriteLibrary');
 *
 *      favorite(alice);  //=> undefined
 *      favoriteWithDefault(alice);  //=> 'Ramda'
 */

/**
 * this is ramda sig version
 */
// function propOr<Value, Prop extends keyof Value, Fallback = any>(
//   val: AnyArrayOrObj,
//   prop: Prop,
//   obj: Fallback
// ) {
//   // @note - with ts, figured out this `has` was backwards eh?
//   // return has(prop, obj) ? obj[prop] : val
//   return has(obj, prop) ? obj[prop] : val
// }

function propOr<Obj, Prop extends keyof Obj, Fallback = any>(
  obj: Obj,
  prop: Prop,
  fallback: Fallback
) {
  return has(obj, prop) ? obj[prop] : fallback
}

export default curry(3, propOr)