initial commit
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
sudo: false
|
||||
language: node_js
|
||||
notifications:
|
||||
email: false
|
||||
node_js:
|
||||
- 6
|
||||
- 0.12
|
||||
- 0.11
|
||||
- 0.10
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Olivier Lalonde <olalonde@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
# to-object-reducer
|
||||
|
||||
[](https://travis-ci.org/olalonde/to-object-reducer)
|
||||
|
||||
Simple JavaScript/Node.js module which exports a `toObject`
|
||||
reducer that can be used with `arr.reduce(toObject)` to convert an array
|
||||
`[[key,value],...]` to an object `{ key: value, ... }`.
|
||||
|
||||
Example:
|
||||
|
||||
```javascript
|
||||
[['username', 'olalonde'], ['id',1337]].reduce(toObj, {})
|
||||
// => { username: 'olalonde', id: 1337 }
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install --save to-object-reducer
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
It is meant to facilitate the pattern of converting an object to an
|
||||
array of `[key,val]` for easier manipulation with Array methods and back
|
||||
to an object, e.g.:
|
||||
|
||||
```javascript
|
||||
const toObject = require('to-object-reducer')
|
||||
|
||||
// Lower case object keys
|
||||
const lowerCaseHeaders = (headers = {}) => Object
|
||||
.keys(headers)
|
||||
.map(key => [key, headers[key]])
|
||||
.map([key, val] => [key.toLowerCase(), val])
|
||||
.reduce(toObject, {})
|
||||
|
||||
// Or with ES2017 Object.entries
|
||||
const lowerCaseHeaders = (headers = {}) => Object
|
||||
.entries(headers)
|
||||
.map([key, val] => [key.toLowerCase(), val])
|
||||
.reduce(toObject, {})
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
module.exports = function (obj, kvArr) {
|
||||
var key = kvArr[0]
|
||||
var value = kvArr[1]
|
||||
obj[key] = value
|
||||
return obj
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "to-object-reducer",
|
||||
"version": "1.0.1",
|
||||
"description": "Simple array reducer function that turns an array of [[key, val],...] to an object { key: val, ... }",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "tape test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/olalonde/to-object-reducer.git"
|
||||
},
|
||||
"author": "Oli Lalonde <olalonde@gmail.com> (https://syskall.com)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/olalonde/to-object-reducer/issues"
|
||||
},
|
||||
"homepage": "https://github.com/olalonde/to-object-reducer#readme",
|
||||
"devDependencies": {
|
||||
"tape": "^4.6.0"
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
var test = require('tape')
|
||||
|
||||
var toObject = require('./')
|
||||
|
||||
var arr = [
|
||||
['id', 1337],
|
||||
['username', 'olalonde'],
|
||||
['email', 'olalonde@gmail.com'],
|
||||
]
|
||||
|
||||
test(function (t) {
|
||||
var obj = arr.reduce(toObject)
|
||||
t.deepEqual(obj, {
|
||||
id: 1337,
|
||||
username: 'olalonde',
|
||||
email: 'olalonde@gmail.com',
|
||||
})
|
||||
t.end()
|
||||
})
|
||||
Reference in New Issue
Block a user