From 6f6288b4235bafa4a401b0bbe99aa7551984b9a5 Mon Sep 17 00:00:00 2001 From: Joel Alejandro Villarreal Bertoldi Date: Sat, 15 Apr 2017 02:57:05 -0300 Subject: [PATCH] initial commit --- README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 29 ++++++++++++++++++++++++ package.json | 26 ++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md index b48049a..4a0f7f7 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/index.js b/index.js new file mode 100644 index 0000000..2cef695 --- /dev/null +++ b/index.js @@ -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); + }; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..4024f11 --- /dev/null +++ b/package.json @@ -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 ", + "license": "MIT", + "bugs": { + "url": "https://github.com/joelalejandro/feathers-hooks-csvtoarray/issues" + }, + "homepage": "https://github.com/joelalejandro/feathers-hooks-csvtoarray#readme" +}