diff --git a/CHANGELOG.md b/CHANGELOG.md index 397882a..2e301d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [5.2.0](https://github.com/flybondi/ramda-land/compare/v5.1.0...v5.2.0) (2023-11-12) + + +### Features + +* **stringProp:** add helper for when you always need a string ([#150](https://github.com/flybondi/ramda-land/issues/150)) ([d02385d](https://github.com/flybondi/ramda-land/commit/d02385d0fa343b16997cce0284c0178c5f19b072)) + # [5.1.0](https://github.com/flybondi/ramda-land/compare/v5.0.1...v5.1.0) (2023-10-30) diff --git a/docs/all-to-lower-upper.js.html b/docs/all-to-lower-upper.js.html index e0cbdf2..8166253 100644 --- a/docs/all-to-lower-upper.js.html +++ b/docs/all-to-lower-upper.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -103,7 +103,7 @@

all-to-lower-upper.js


diff --git a/docs/always-new.js.html b/docs/always-new.js.html index deef001..ea0a55c 100644 --- a/docs/always-new.js.html +++ b/docs/always-new.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -88,7 +88,7 @@

always-new.js


diff --git a/docs/apply-spec.js.html b/docs/apply-spec.js.html index a38b276..c39401c 100644 --- a/docs/apply-spec.js.html +++ b/docs/apply-spec.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -129,7 +129,7 @@

apply-spec.js


diff --git a/docs/array-path.js.html b/docs/array-path.js.html index 3d1c0d2..f0828f6 100644 --- a/docs/array-path.js.html +++ b/docs/array-path.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -117,7 +117,7 @@

array-path.js


diff --git a/docs/array-prop.js.html b/docs/array-prop.js.html index 5f9a7a9..2b3a7d8 100644 --- a/docs/array-prop.js.html +++ b/docs/array-prop.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -117,7 +117,7 @@

array-prop.js


diff --git a/docs/boolean-prop.js.html b/docs/boolean-prop.js.html index b66cea8..ecb933b 100644 --- a/docs/boolean-prop.js.html +++ b/docs/boolean-prop.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -125,7 +125,7 @@

boolean-prop.js


diff --git a/docs/capitalize-words.js.html b/docs/capitalize-words.js.html index 5492072..1b7a0bd 100644 --- a/docs/capitalize-words.js.html +++ b/docs/capitalize-words.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -89,7 +89,7 @@

capitalize-words.js


diff --git a/docs/capitalize.js.html b/docs/capitalize.js.html index a61de19..2f3a99b 100644 --- a/docs/capitalize.js.html +++ b/docs/capitalize.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -84,7 +84,7 @@

capitalize.js


diff --git a/docs/cast-array.js.html b/docs/cast-array.js.html index 1305230..cca5bff 100644 --- a/docs/cast-array.js.html +++ b/docs/cast-array.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -86,7 +86,7 @@

cast-array.js


diff --git a/docs/coalesce.js.html b/docs/coalesce.js.html index 07f5c02..a8644a4 100644 --- a/docs/coalesce.js.html +++ b/docs/coalesce.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -102,7 +102,7 @@

coalesce.js


diff --git a/docs/compact-spec.js.html b/docs/compact-spec.js.html index 72b31e8..4b54274 100644 --- a/docs/compact-spec.js.html +++ b/docs/compact-spec.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -100,7 +100,7 @@

compact-spec.js


diff --git a/docs/compact.js.html b/docs/compact.js.html index 2c1a2bb..c2854b3 100644 --- a/docs/compact.js.html +++ b/docs/compact.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -87,7 +87,7 @@

compact.js


diff --git a/docs/curry-n.js.html b/docs/curry-n.js.html index c9f44a4..b2e84b1 100644 --- a/docs/curry-n.js.html +++ b/docs/curry-n.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -117,7 +117,7 @@

curry-n.js


diff --git a/docs/curry.js.html b/docs/curry.js.html index 4186aae..0174e75 100644 --- a/docs/curry.js.html +++ b/docs/curry.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -111,7 +111,7 @@

curry.js


diff --git a/docs/global.html b/docs/global.html index f94abfc..4516f61 100644 --- a/docs/global.html +++ b/docs/global.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -8783,6 +8783,836 @@
Returns:
+

stringPath(propPath, obj) → {string}

+ + + + + + +
+ +
Description:
+
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + +
Example
+ +
stringPath(['life', 'count'], { life: { count: 1 }}); // '1'
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
propPath + + +string +| + +Object + + + + +

Path to the property to extract. Also accepts a +property name as a single string.

obj + + +object + + + + +

Source of the extracted property.

+ + + + + + + + + + + + + + + + +
Returns:
+ + +
+

The value of obj at propPath as a string or null.

+
+ + + +
+
+ Type +
+
+ +string + + + +
+
+ + + + + + + + + + +

stringPathOr(defaultValue, propPath, obj) → {*}

+ + + + + + +
+ +
Description:
+
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + +
Example
+ +
stringPathOr('default', ['life', 'reason'], { foo: 'bar' }); // 'default'
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
defaultValue + + +* + + + + +

The value to return if propPath does not exist in obj +or its value is nil or empty.

propPath + + +string +| + +Object + + + + +

Path to the property to extract. Also accepts a +property name as a single string.

obj + + +object + + + + +

Source of the extracted property.

+ + + + + + + + + + + + + + + + +
Returns:
+ + +
+

The value of obj at propName as a string or defaultValue.

+
+ + + +
+
+ Type +
+
+ +* + + + +
+
+ + + + + + + + + + +

stringProp(propName, obj) → {string}

+ + + + + + +
+ +
Description:
+
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
propName + + +string + + + + +

Name of the property to extract.

obj + + +object + + + + +

Source of the extracted property.

+ + + + + + + + + + + + + + + + +
Returns:
+ + +
+

The value of obj at propName as a string or null string.

+
+ + + +
+
+ Type +
+
+ +string + + + +
+
+ + + + + + + + + + +

stringPropOr(defaultValue, propName, obj) → {*}

+ + + + + + +
+ +
Description:
+
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
defaultValue + + +* + + + + +

The value to return if propName does not exist in obj +or is nil.

propName + + +string + + + + +

Name of the property to extract.

obj + + +object + + + + +

Source of the extracted property.

+ + + + + + + + + + + + + + + + +
Returns:
+ + +
+

The value of obj at propName as a string or defaultValue.

+
+ + + +
+
+ Type +
+
+ +* + + + +
+
+ + + + + + + + + +

sumProp(precision, propName, values) → {number}

@@ -9563,7 +10393,7 @@
Returns:

diff --git a/docs/index.html b/docs/index.html index 9f572e1..332d22f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -120,7 +120,7 @@

Ramda overlap


diff --git a/docs/interpolate.js.html b/docs/interpolate.js.html index 1cfe9fd..23ee236 100644 --- a/docs/interpolate.js.html +++ b/docs/interpolate.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -96,7 +96,7 @@

interpolate.js


diff --git a/docs/is-nil-empty.js.html b/docs/is-nil-empty.js.html index 6194d73..65d6548 100644 --- a/docs/is-nil-empty.js.html +++ b/docs/is-nil-empty.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -145,7 +145,7 @@

is-nil-empty.js


diff --git a/docs/is-one-of.js.html b/docs/is-one-of.js.html index 7bd4a48..6e01b12 100644 --- a/docs/is-one-of.js.html +++ b/docs/is-one-of.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -115,7 +115,7 @@

is-one-of.js


diff --git a/docs/join-from.js.html b/docs/join-from.js.html index e99f3ac..41b4d0d 100644 --- a/docs/join-from.js.html +++ b/docs/join-from.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -93,7 +93,7 @@

join-from.js


diff --git a/docs/lower-equals.js.html b/docs/lower-equals.js.html index 8be7bb1..5205035 100644 --- a/docs/lower-equals.js.html +++ b/docs/lower-equals.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -85,7 +85,7 @@

lower-equals.js


diff --git a/docs/lower-path-satisfies.js.html b/docs/lower-path-satisfies.js.html index 964bcf2..47aea5d 100644 --- a/docs/lower-path-satisfies.js.html +++ b/docs/lower-path-satisfies.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -90,7 +90,7 @@

lower-path-satisfies.js


diff --git a/docs/lower-path.js.html b/docs/lower-path.js.html index 3207c45..a4773fe 100644 --- a/docs/lower-path.js.html +++ b/docs/lower-path.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -86,7 +86,7 @@

lower-path.js


diff --git a/docs/lower-trim.js.html b/docs/lower-trim.js.html index 407a3a0..5fa083f 100644 --- a/docs/lower-trim.js.html +++ b/docs/lower-trim.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -80,7 +80,7 @@

lower-trim.js


diff --git a/docs/map-keys.js.html b/docs/map-keys.js.html index 94b19e8..41be04c 100644 --- a/docs/map-keys.js.html +++ b/docs/map-keys.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -81,7 +81,7 @@

map-keys.js


diff --git a/docs/merge-into.js.html b/docs/merge-into.js.html index 0a3f8cf..4ccfe7b 100644 --- a/docs/merge-into.js.html +++ b/docs/merge-into.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -110,7 +110,7 @@

merge-into.js


diff --git a/docs/merge-spec.js.html b/docs/merge-spec.js.html index 4ee7cd1..047276d 100644 --- a/docs/merge-spec.js.html +++ b/docs/merge-spec.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -97,7 +97,7 @@

merge-spec.js


diff --git a/docs/n-ary.js.html b/docs/n-ary.js.html index 2009277..964855a 100644 --- a/docs/n-ary.js.html +++ b/docs/n-ary.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -144,7 +144,7 @@

n-ary.js


diff --git a/docs/not-equals.js.html b/docs/not-equals.js.html index 94fa2a0..d498a93 100644 --- a/docs/not-equals.js.html +++ b/docs/not-equals.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -85,7 +85,7 @@

not-equals.js


diff --git a/docs/number-prop.js.html b/docs/number-prop.js.html index 8349f69..d4b84c9 100644 --- a/docs/number-prop.js.html +++ b/docs/number-prop.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -174,7 +174,7 @@

number-prop.js


diff --git a/docs/prop-of.js.html b/docs/prop-of.js.html index ebd2cfa..6c2d108 100644 --- a/docs/prop-of.js.html +++ b/docs/prop-of.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -86,7 +86,7 @@

prop-of.js


diff --git a/docs/reject-nil.js.html b/docs/reject-nil.js.html index 9084b1d..3585b14 100644 --- a/docs/reject-nil.js.html +++ b/docs/reject-nil.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -103,7 +103,7 @@

reject-nil.js


diff --git a/docs/rename-keys.js.html b/docs/rename-keys.js.html index c20e30d..3ff8d36 100644 --- a/docs/rename-keys.js.html +++ b/docs/rename-keys.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -88,7 +88,7 @@

rename-keys.js


diff --git a/docs/round.js.html b/docs/round.js.html index 9517817..5567295 100644 --- a/docs/round.js.html +++ b/docs/round.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -97,7 +97,7 @@

round.js


diff --git a/docs/spread-prop.js.html b/docs/spread-prop.js.html index d2740a2..0c63829 100644 --- a/docs/spread-prop.js.html +++ b/docs/spread-prop.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -84,7 +84,7 @@

spread-prop.js


diff --git a/docs/string-prop.js.html b/docs/string-prop.js.html new file mode 100644 index 0000000..d324d41 --- /dev/null +++ b/docs/string-prop.js.html @@ -0,0 +1,179 @@ + + + + + + string-prop.js - Documentation + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

string-prop.js

+ + + + + + + +
+
+
import {
+  always,
+  compose,
+  identity,
+  isEmpty,
+  isNil,
+  path,
+  prop,
+  unless,
+  useWith,
+  when,
+  is
+} from 'ramda';
+import castArray from './cast-array';
+import curryN from './curry-n';
+import { isNilOrEmpty } from './is-nil-empty';
+
+/**
+ * @function
+ * @private
+ */
+const stringUnlessNilOrEmpty = compose(
+  unless(isNil, String),
+  when(isEmpty, always(null)),
+  when(is(Object), JSON.stringify)
+);
+
+/**
+ * Shorthand function to extract a property from an object and convert it to a string.
+ * If the property is `null` or `undefined` it will be returned as is; if the property is
+ * an empty string (`''`), `null` will be returned.
+ *
+ * @function
+ * @see https://ramdajs.com/docs/#prop
+ * @param {string} propName Name of the property to extract.
+ * @param {object} obj Source of the extracted property.
+ * @returns {string} The value of `obj` at `propName` as a string or null string.
+ */
+export const stringProp = curryN(2, compose(stringUnlessNilOrEmpty, prop));
+
+/**
+ * Shorthand function to extract a nested property from an object and convert it to a string.
+ * If the property is `null` or `undefined` it will be returned as is; if the property is
+ * an empty string (`''`), `null` will be returned.
+ *
+ * @example
+ *  stringPath(['life', 'count'], { life: { count: 1 }}); // '1'
+ *
+ * @function
+ * @see https://ramdajs.com/docs/#path
+ * @param {string|{String[]}} propPath Path to the property to extract. Also accepts a
+ *  property name as a single string.
+ * @param {object} obj Source of the extracted property.
+ * @returns {string} The value of `obj` at `propPath` as a string or `null`.
+ */
+export const stringPath = curryN(
+  2,
+  compose(stringUnlessNilOrEmpty, useWith(path, [castArray, identity]))
+);
+
+/**
+ * Extract a property from an object and convert it to a string. If property
+ * is absent, `null`, `undefined` or empty, the default value will be returned instead.
+ *
+ * @function
+ * @see https://ramdajs.com/docs/#propOr
+ * @param {*} defaultValue The value to return if `propName` does not exist in `obj`
+ *  or is nil.
+ *  @param {string} propName Name of the property to extract.
+ * @param {object} obj Source of the extracted property.
+ * @returns {*} The value of `obj` at `propName` as a string or `defaultValue`.
+ */
+export const stringPropOr = curryN(3, function stringPropOr(defaultValue, propName, obj) {
+  const value = stringProp(propName, obj);
+  return isNilOrEmpty(value) ? defaultValue : value;
+});
+
+/**
+ * Extract a nested property from an object and convert it to a string. If property
+ * is absent, `null`, `undefined` or empty, the default value will be returned instead.
+ *
+ * @example
+ *  stringPathOr('default', ['life', 'reason'], { foo: 'bar' }); // 'default'
+ *
+ * @function
+ * @see https://ramdajs.com/docs/#pathOr
+ * @param {*} defaultValue The value to return if `propPath` does not exist in `obj`
+ *  or its value is nil or empty.
+ *  @param {string|{String[]}} propPath Path to the property to extract. Also accepts a
+ *  property name as a single string.
+ * @param {object} obj Source of the extracted property.
+ * @returns {*} The value of `obj` at `propName` as a string or `defaultValue`.
+ */
+export const stringPathOr = curryN(3, function stringPathOr(defaultValue, propPath, obj) {
+  const value = stringPath(propPath, obj);
+  return isNilOrEmpty(value) ? defaultValue : value;
+});
+
+
+
+ + + + + + +
+ +
+ + + + + + + + + + + + + diff --git a/docs/sum-prop.js.html b/docs/sum-prop.js.html index ce9defa..1a9d99e 100644 --- a/docs/sum-prop.js.html +++ b/docs/sum-prop.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -100,7 +100,7 @@

sum-prop.js


diff --git a/docs/throw-error.js.html b/docs/throw-error.js.html index 96bb6f0..31696eb 100644 --- a/docs/throw-error.js.html +++ b/docs/throw-error.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -104,7 +104,7 @@

throw-error.js


diff --git a/docs/truncate.js.html b/docs/truncate.js.html index 1f64d2a..cacae9a 100644 --- a/docs/truncate.js.html +++ b/docs/truncate.js.html @@ -37,7 +37,7 @@ -

Home

Global

+

Home

Global

@@ -129,7 +129,7 @@

truncate.js


diff --git a/package.json b/package.json index d21e3ea..824d54a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@flybondi/ramda-land", - "version": "5.1.0", + "version": "5.2.0", "description": "A collection of reusable functions for ramda", "keywords": [ "ramda",