From 9959bf0194bf780fa54ab74585c75af9fac29ae7 Mon Sep 17 00:00:00 2001 From: Dale Francis Date: Sat, 20 Jul 2019 22:27:03 -0700 Subject: [PATCH] Changing toPairs to return an array (#430) --- docs/src/pages/docs/crocks/Pair.md | 11 +++++------ docs/src/pages/docs/functions/helpers.md | 16 ++++++++-------- docs/src/pages/docs/functions/index.md | 2 +- src/Pair/toPairs.js | 7 +++---- src/Pair/toPairs.spec.js | 8 ++++---- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/docs/src/pages/docs/crocks/Pair.md b/docs/src/pages/docs/crocks/Pair.md index a014e72b1..409442d0f 100644 --- a/docs/src/pages/docs/crocks/Pair.md +++ b/docs/src/pages/docs/crocks/Pair.md @@ -863,8 +863,8 @@ toPairs :: Object -> List (Pair String a) ``` When dealing with `Object`s, sometimes it makes more sense to work in -a `Foldable` structure like a `List` of key-value `Pair`s. `toPairs` provides -a means to take an object and give you back a `List` of `Pairs` that have +a `Foldable` structure like an `Array` of key-value `Pair`s. `toPairs` provides a +means to take an object and give you back an `Array` of `Pairs` that have a `String` that represents the key in the `fst` and the value for that key in the `snd`. The primitive values are copied, while non-primitive values are references. Like most of the `Object` functions in `crocks`, any keys @@ -888,10 +888,9 @@ const record = { const joinField = (key, value) => `${key}:${value}` -// joinRecord :: List String -> String -const joinRecord = list => - list.toArray() - .join('|') +// joinRecord :: Array String -> String +const joinRecord = arr => + arr.join('|') // buildRecord :: Object -> String const buildRecord = compose( diff --git a/docs/src/pages/docs/functions/helpers.md b/docs/src/pages/docs/functions/helpers.md index 4dd86e75f..fb8de756f 100644 --- a/docs/src/pages/docs/functions/helpers.md +++ b/docs/src/pages/docs/functions/helpers.md @@ -432,14 +432,14 @@ As a `b` can be an `a` as well. fromPairs :: Foldable f => f (Pair String a) -> Object ``` -As an inverse to [`toPairs`][topairs], `fromPairs` takes either -an `Array` or `List` of key-value `Pair`s and constructs an `Object` from it. -The `Pair` must contain a `String` in the `fst` and any type of value in -the `snd`. The `fst` will become the key for the value in the `snd`. All -primitive values are copied into the new `Object`, while non-primitives are -references to the original. If you provide an `undefined` values for the second, -that `Pair` will not be represented in the resulting `Object`. Also, when if -multiple keys share the same name, that last value will be moved over. +As an inverse to [`toPairs`][topairs], `fromPairs` takes either a `Foldable` of +key-value `Pair`s and constructs an `Object` from it. The `Pair` must +contain a `String` in the `fst` and any type of value in the `snd`. The `fst` +will become the key for the value in the `snd`. All primitive values are copied +into the new `Object`, while non-primitives are references to the original. If +you provide an `undefined` values for the second, that `Pair` will not be +represented in the resulting `Object`. Also, when if multiple keys share the +same name, that last value will be moved over. #### getPathOr diff --git a/docs/src/pages/docs/functions/index.md b/docs/src/pages/docs/functions/index.md index 3bf828864..2529052bf 100644 --- a/docs/src/pages/docs/functions/index.md +++ b/docs/src/pages/docs/functions/index.md @@ -104,7 +104,7 @@ need to account for for the rest of your flow. | [`setPath`][setpath] | [ (String | Integer) ] -> a -> (Object | Array) -> (Object | Array) | `crocks/helpers/setPath` | | [`setProp`][setprop] | (String | Integer) -> a -> (Object | Array) -> (Object | Array) | `crocks/helpers/setProp` | | [`tap`][tap] | `(a -> b) -> a -> a` | `crocks/helpers/tap` | -| [`toPairs`][topairs] | `Object -> List (Pair String a)` | `crocks/Pair/toPairs` | +| [`toPairs`][topairs] | `Object -> Array (Pair String a)` | `crocks/Pair/toPairs` | | [`tryCatch`][trycatch] | `((*) -> b) -> (*) -> Result e b` | `crocks/Result/tryCatch` | | [`unary`][unary] | `((*) -> b) -> a -> b` | `crocks/helpers/unary` | | [`unit`][unit] | `() -> undefined` | `crocks/helpers/unit` | diff --git a/src/Pair/toPairs.js b/src/Pair/toPairs.js index 6dad63574..444363e70 100644 --- a/src/Pair/toPairs.js +++ b/src/Pair/toPairs.js @@ -1,11 +1,10 @@ /** @license ISC License (c) copyright 2017 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ -const List = require('../core/List') const Pair = require('../core/Pair') const isObject = require('../core/isObject') -/** toPairs :: Object -> List (Pair String a) */ +// toPairs : Object -> Array (Pair String a) function toPairs(obj) { if(!isObject(obj)) { throw new TypeError('toPairs: Object required for argument') @@ -13,9 +12,9 @@ function toPairs(obj) { return Object.keys(obj).reduce( (acc, key) => obj[key] !== undefined - ? acc.concat(List.of(Pair(key, obj[key]))) + ? acc.concat([ Pair(key, obj[key]) ]) : acc, - List.empty() + [] ) } diff --git a/src/Pair/toPairs.spec.js b/src/Pair/toPairs.spec.js index df4b6c996..b55b19ab1 100644 --- a/src/Pair/toPairs.spec.js +++ b/src/Pair/toPairs.spec.js @@ -3,11 +3,11 @@ const helpers = require('../test/helpers') const bindFunc = helpers.bindFunc -const List = require('../core/List') const Pair = require('../core/Pair') const isSameType = require('../core/isSameType') +const find = require('../Maybe/find') const unit = require('../core/_unit') - +const isArray = require('../core/isArray') const toPairs = require('./toPairs') test('toPairs', t => { @@ -39,9 +39,9 @@ test('toPairs', t => { const result = toPairs(data) const allPairs = result.reduce((acc, x) => acc && isSameType(Pair, x), true) - const getPair = key => result.filter(x => x.fst() === key).head().option(Pair(null, 'No Key')) + const getPair = key => find(x => x.fst() === key, result).option(Pair(null, 'No Key')) - t.ok(isSameType(List, result), 'returns a List') + t.ok(isArray(result), 'returns an Array') t.ok(allPairs, 'list contains all Pairs') t.equals(getPair('undef').snd(), 'No Key', 'does not include keys with undefined values')