From d44612a8ba124baff2ecd77c175254d81723e572 Mon Sep 17 00:00:00 2001 From: Michael Albert Date: Wed, 31 Aug 2016 22:58:14 +0200 Subject: [PATCH] recursive processing of arrays --- .gitignore | 4 +++- Cakefile | 2 +- gulpfile.js | 28 ++++++++++++++++++++++++++++ package.json | 16 +++++++++++----- src/morph.coffee | 18 +++++++++++++++++- test/main.coffee | 7 +++++++ 6 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 gulpfile.js diff --git a/.gitignore b/.gitignore index eeee7ca..6b7aa34 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules .DS_Store npm-debug.log -lib/ \ No newline at end of file +lib/ + +.idea/** diff --git a/Cakefile b/Cakefile index 8a1be45..0cc348c 100644 --- a/Cakefile +++ b/Cakefile @@ -39,4 +39,4 @@ build = (watch) -> coffee = spawn 'coffee', options coffee.stdout.on 'data', (data) -> print data - coffee.stderr.on 'data', (data) -> print data \ No newline at end of file + coffee.stderr.on 'data', (data) -> print data diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..0bb50c8 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,28 @@ +const gulp = require('gulp'); +const gutil = require('gulp-util'); +const mocha = require('gulp-mocha'); +const coffee = require('gulp-coffee'); + +gulp.task('coffee', function() { + gulp.src('./src/*.coffee') + .pipe(coffee({bare: true}).on('error', gutil.log)) + .pipe(gulp.dest('./lib/')); +}); + +gulp.task('coffee-test', function() { + gulp.src('./test/*.coffee') + .pipe(coffee({bare: true}).on('error', gutil.log)) + .pipe(gulp.dest('./test/')); + + // gulp.src('./test/*.coffee') + // .pipe(coffee({bare: true}).on('error', gutil.log)) + // .pipe(gulp.dest('./test/')); +}); + + + +gulp.task('test', function() { + gulp.src('./test/main.js', {read: false}) + // gulp-mocha needs filepaths so you can't have any plugins before it + .pipe(mocha({reporter: 'nyan'})) +}); diff --git a/package.json b/package.json index c9a5e3e..3f4b46c 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "description": "Lightweight string transformations", "main": "lib/morph.js", "scripts": { - "prepublish" : "cake build", + "prepublish": "cake build", "test": "cake test" }, "repository": { "type": "git", - "url": "https://github.com/cmoncrief/morph.git" + "url": "https://github.com/cmoncrief/morph.git" }, "keywords": [ "string", @@ -25,8 +25,14 @@ "author": "Charles Moncrief", "license": "MIT", "devDependencies": { - "mocha": "~1.8.1", - "coffee-script": "~1.4.0" + "coffee-script": "~1.10.0", + "gulp": "^3.9.1", + "gulp-coffee": "^2.3.2", + "gulp-mocha": "^3.0.1", + "gulp-util": "^3.0.7", + "mocha": "~3.0.2" }, - "engines": { "node" : "*"} + "engines": { + "node": "*" + } } diff --git a/src/morph.coffee b/src/morph.coffee index 7ec439e..bac557a 100644 --- a/src/morph.coffee +++ b/src/morph.coffee @@ -5,7 +5,23 @@ # Private function. Converts all object keys using the passed in function. morphObj = (input, caller, cap) -> newObj = {} - newObj[caller(key, cap)] = value for key, value of input + + if Array.isArray(input) + newArray = [] + for inputItem in input + newValue = caller(inputItem, cap) + newArray.push newValue + + return newArray + + for key, value of input + newObj[caller(key, cap)] = value + if typeof value == 'object' + newValue = caller(value, cap) + else + newValue = value + + newObj[caller(key, cap)] = newValue; return newObj # Private function. Capitalizes the first letter of the first word. diff --git a/test/main.coffee b/test/main.coffee index 74e6b09..5c018c6 100644 --- a/test/main.coffee +++ b/test/main.coffee @@ -1,6 +1,7 @@ assert = require 'assert' morph = require '../lib/morph' + describe 'Snake case', -> it 'should convert camel case to snake case', -> @@ -263,3 +264,9 @@ describe 'Object keys', -> assert tmp["Lorem Ipsum Dolor"] assert tmp["Sit Amet"] +describe 'Recursive array to Snake case', -> + + it 'should convert aray with camel case to snake case', -> + output = morph.toSnake {itemId: 1, children: [{childName: 'Georges'}, {childName: 'Steve'}]} + assert.equal output.item_id, 1 + assert.equal output.children[0].child_name, 'Georges'