File tree Expand file tree Collapse file tree 3 files changed +118
-0
lines changed Expand file tree Collapse file tree 3 files changed +118
-0
lines changed Original file line number Diff line number Diff line change 11# feathers-hooks-csvtoarray
22Feathers hook for converting a comma-delimited list to an Array of strings.
3+
4+ ## Installing
5+
6+ Simply run ` npm install --save feathers-hooks-csvtoarray ` and you're good to go!
7+
8+ ## Usage
9+
10+ > ** 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.
11+
12+ Require the hook:
13+
14+ ``` js
15+ const csvToArray = require (' feathers-hooks-csvtoarray' );
16+ ```
17+
18+ Then, choose which fields in your Sequelize Model should be parsed by the hook, adding the option ` csvAsArray: true ` :
19+
20+ ``` js
21+ // things.model.js
22+ const Sequelize = require (' sequelize' );
23+
24+ module .exports = function (app ) {
25+ const sequelizeClient = app .get (' sequelizeClient' );
26+ const things = sequelizeClient .define (' things' , {
27+ id: {
28+ type: Sequelize .CHAR (100 ),
29+ allowNull: false ,
30+ primaryKey: true
31+ },
32+ names: {
33+ type: Sequelize .CHAR (200 ),
34+ allowNull: false ,
35+ csvAsArray: true // this will be parsed by csvtoarray
36+ }, {}
37+ });
38+
39+ return things;
40+ };
41+ ```
42+
43+ Finally, bind the hook to the model's service.
44+
45+ ``` js
46+ app .service (' things' ).hooks ({
47+ after: {
48+ find: [ csvToArray () ],
49+ get: [ csvToArray () ]
50+ }
51+ });
52+ ```
53+
54+ ## TODOs
55+
56+ Check out the [ issues] ( https://github.com/joelalejandro/feathers-hooks-csvtoarray/issues ) .
57+
58+ ## Feel like contributing?
59+
60+ Knock yourself out! Fork the repo and make a PR.
61+
62+ ## Licence
63+
64+ MIT
65+
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ function csvToArray ( value ) {
4+ return value . split ( ',' ) . map ( function ( item ) { return item . trim ( ) ; } ) ;
5+ }
6+
7+ function applyCsvToArrayTransform ( data , model ) {
8+ Object . keys ( model . attributes ) . forEach ( function ( key ) {
9+ const attribute = model . attributes [ key ] ;
10+ if ( ! attribute . csvAsArray ) {
11+ return ;
12+ }
13+ if ( Array . isArray ( data ) ) {
14+ data . forEach ( function ( item , index ) {
15+ data [ index ] . setDataValue ( key , csvToArray ( item . getDataValue ( key ) ) ) ;
16+ } ) ;
17+ } else {
18+ data . setDataValue ( key , csvToArray ( data . getDataValue ( key ) ) ) ;
19+ }
20+ } ) ;
21+ return data ;
22+ }
23+
24+ module . exports = function ( options = { } ) { // eslint-disable-line no-unused-vars
25+ return function ( hook ) {
26+ hook . result . data = applyCsvToArrayTransform ( hook . result . data , hook . service . Model ) ;
27+ return Promise . resolve ( hook ) ;
28+ } ;
29+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " feathers-hooks-csvtoarray" ,
3+ "version" : " 0.0.1" ,
4+ "description" : " Feathers hook for converting a comma-delimited list to an Array of strings." ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1"
8+ },
9+ "repository" : {
10+ "type" : " git" ,
11+ "url" : " git+https://github.com/joelalejandro/feathers-hooks-csvtoarray.git"
12+ },
13+ "keywords" : [
14+ " feathers" ,
15+ " hook" ,
16+ " csv" ,
17+ " array" ,
18+ " string"
19+ ],
20+ "author" : " Joel A. Villarreal Bertoldi <joel.a.villarreal@gmail.com>" ,
21+ "license" : " MIT" ,
22+ "bugs" : {
23+ "url" : " https://github.com/joelalejandro/feathers-hooks-csvtoarray/issues"
24+ },
25+ "homepage" : " https://github.com/joelalejandro/feathers-hooks-csvtoarray#readme"
26+ }
You can’t perform that action at this time.
0 commit comments