Skip to content

Commit

Permalink
Changing toPairs to return an array (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefrancis88 authored and evilsoft committed Oct 9, 2020
1 parent 1ff50f7 commit 9959bf0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
11 changes: 5 additions & 6 deletions docs/src/pages/docs/crocks/Pair.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
16 changes: 8 additions & 8 deletions docs/src/pages/docs/functions/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/docs/functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ need to account for for the rest of your flow.
| [`setPath`][setpath] | <code>[ (String &#124; Integer) ] -> a -> (Object &#124; Array) -> (Object &#124; Array)</code> | `crocks/helpers/setPath` |
| [`setProp`][setprop] | <code>(String &#124; Integer) -> a -> (Object &#124; Array) -> (Object &#124; Array)</code> | `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` |
Expand Down
7 changes: 3 additions & 4 deletions src/Pair/toPairs.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/** @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')
}

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()
[]
)
}

Expand Down
8 changes: 4 additions & 4 deletions src/Pair/toPairs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 9959bf0

Please sign in to comment.