Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joelalejandro committed Apr 15, 2017
1 parent a33d32d commit 6f6288b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
63 changes: 63 additions & 0 deletions README.md
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

29 changes: 29 additions & 0 deletions index.js
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);
};
};
26 changes: 26 additions & 0 deletions package.json
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"
}

0 comments on commit 6f6288b

Please sign in to comment.