-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a33d32d
commit 6f6288b
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,65 @@ | ||
# feathers-hooks-csvtoarray | ||
Feathers hook for converting a comma-delimited list to an Array of strings. | ||
|
||
## Installing | ||
|
||
Simply run `npm install --save feathers-hooks-csvtoarray` and you're good to go! | ||
|
||
## Usage | ||
|
||
> **Important:** As of version 0.1.0, this hook is intended to use with Sequelize. Further versions will move the coupling code to hook configurations. | ||
Require the hook: | ||
|
||
```js | ||
const csvToArray = require('feathers-hooks-csvtoarray'); | ||
``` | ||
|
||
Then, choose which fields in your Sequelize Model should be parsed by the hook, adding the option `csvAsArray: true`: | ||
|
||
```js | ||
// things.model.js | ||
const Sequelize = require('sequelize'); | ||
|
||
module.exports = function (app) { | ||
const sequelizeClient = app.get('sequelizeClient'); | ||
const things = sequelizeClient.define('things', { | ||
id: { | ||
type: Sequelize.CHAR(100), | ||
allowNull: false, | ||
primaryKey: true | ||
}, | ||
names: { | ||
type: Sequelize.CHAR(200), | ||
allowNull: false, | ||
csvAsArray: true // this will be parsed by csvtoarray | ||
}, {} | ||
}); | ||
|
||
return things; | ||
}; | ||
``` | ||
|
||
Finally, bind the hook to the model's service. | ||
|
||
```js | ||
app.service('things').hooks({ | ||
after: { | ||
find: [ csvToArray() ], | ||
get: [ csvToArray() ] | ||
} | ||
}); | ||
``` | ||
|
||
## TODOs | ||
|
||
Check out the [issues](https://github.com/joelalejandro/feathers-hooks-csvtoarray/issues). | ||
|
||
## Feel like contributing? | ||
|
||
Knock yourself out! Fork the repo and make a PR. | ||
|
||
## Licence | ||
|
||
MIT | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
function csvToArray(value) { | ||
return value.split(',').map(function(item) { return item.trim(); }); | ||
} | ||
|
||
function applyCsvToArrayTransform(data, model) { | ||
Object.keys(model.attributes).forEach(function(key) { | ||
const attribute = model.attributes[key]; | ||
if (!attribute.csvAsArray) { | ||
return; | ||
} | ||
if (Array.isArray(data)) { | ||
data.forEach(function(item, index) { | ||
data[index].setDataValue(key, csvToArray(item.getDataValue(key))); | ||
}); | ||
} else { | ||
data.setDataValue(key, csvToArray(data.getDataValue(key))); | ||
} | ||
}); | ||
return data; | ||
} | ||
|
||
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars | ||
return function (hook) { | ||
hook.result.data = applyCsvToArrayTransform(hook.result.data, hook.service.Model); | ||
return Promise.resolve(hook); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "feathers-hooks-csvtoarray", | ||
"version": "0.0.1", | ||
"description": "Feathers hook for converting a comma-delimited list to an Array of strings.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/joelalejandro/feathers-hooks-csvtoarray.git" | ||
}, | ||
"keywords": [ | ||
"feathers", | ||
"hook", | ||
"csv", | ||
"array", | ||
"string" | ||
], | ||
"author": "Joel A. Villarreal Bertoldi <joel.a.villarreal@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/joelalejandro/feathers-hooks-csvtoarray/issues" | ||
}, | ||
"homepage": "https://github.com/joelalejandro/feathers-hooks-csvtoarray#readme" | ||
} |