Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.DS_Store
npm-debug.log
lib/
lib/

.idea/**
2 changes: 1 addition & 1 deletion Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ build = (watch) ->

coffee = spawn 'coffee', options
coffee.stdout.on 'data', (data) -> print data
coffee.stderr.on 'data', (data) -> print data
coffee.stderr.on 'data', (data) -> print data
28 changes: 28 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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'}))
});
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": "*"
}
}
18 changes: 17 additions & 1 deletion src/morph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions test/main.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
assert = require 'assert'
morph = require '../lib/morph'


describe 'Snake case', ->

it 'should convert camel case to snake case', ->
Expand Down Expand Up @@ -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'