Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
imlucas committed Jan 6, 2015
0 parents commit b7576dc
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules/
.lone/
npm-debug.log
*.pyc
171 changes: 171 additions & 0 deletions .jsfmtrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{


"// docs, basically" : "https://github.com/paulirish/sublime-jsfmt#formatting-rules",
"preset": "default",
"plugins": [
"esformatter-quotes",
"esformatter-semicolons",
"esformatter-braces"
],
"quotes": {
"type": "single",
"avoidEscape": false
},
"indent": {
"value": " " // four spaces, b.
},
"whiteSpace" : {
"value" : " ",
"removeTrailing" : 1,

"before" : {
"ArrayExpressionOpening" : 0,
"ArrayExpressionClosing" : 0,
"ArrayExpressionComma" : 0,
"ArgumentComma" : 0,
"ArgumentList" : 0,
"ArgumentListArrayExpression" : 0,
"ArgumentListFunctionExpression" : 0,
"ArgumentListObjectExpression" : 0,
"AssignmentOperator" : 1,
"BinaryExpression": 0,
"BinaryExpressionOperator" : 1,
"BlockComment" : 1,
"CallExpression" : -1,
"CatchParameterList" : 0,
"CatchOpeningBrace" : 1,
"CatchClosingBrace" : 1,
"CatchKeyword" : 1,
"CommaOperator" : 0,
"ConditionalExpressionConsequent" : 1,
"ConditionalExpressionAlternate" : 1,
"DoWhileStatementOpeningBrace" : 1,
"DoWhileStatementClosingBrace" : 1,
"DoWhileStatementConditional" : 1,
"EmptyStatement" : 0,
"ExpressionClosingParentheses" : 0,
"FinallyKeyword" : -1,
"FinallyOpeningBrace" : 1,
"FinallyClosingBrace" : 1,
"ForInStatement" : 1,
"ForInStatementExpressionOpening" : 1,
"ForInStatementExpressionClosing" : 0,
"ForInStatementOpeningBrace" : 1,
"ForInStatementClosingBrace" : 1,
"ForStatement" : 1,
"ForStatementExpressionOpening" : 1,
"ForStatementExpressionClosing" : 0,
"ForStatementOpeningBrace" : 1,
"ForStatementClosingBrace" : 1,
"ForStatementSemicolon" : 0,
"FunctionDeclarationOpeningBrace" : 1,
"FunctionDeclarationClosingBrace" : 1,
"FunctionExpressionOpeningBrace" : 1,
"FunctionExpressionClosingBrace" : 1,
"IfStatementConditionalOpening" : 1,
"IfStatementConditionalClosing" : 0,
"IfStatementOpeningBrace" : 1,
"IfStatementClosingBrace" : 1,
"ElseStatementOpeningBrace" : 1,
"ElseStatementClosingBrace" : 1,
"ElseIfStatementOpeningBrace" : 1,
"ElseIfStatementClosingBrace" : 1,
"MemberExpressionClosing" : 0,
"LineComment" : 1,
"LogicalExpressionOperator" : 1,
"Property" : 1,
"PropertyValue" : 1,
"ParameterComma" : 0,
"ParameterList" : 0,
"SwitchDiscriminantOpening" : 1,
"SwitchDiscriminantClosing" : 0,
"ThrowKeyword": 1,
"TryKeyword": -1,
"TryOpeningBrace" : 1,
"TryClosingBrace" : 1,
"UnaryExpressionOperator": 0,
"VariableName" : 1,
"VariableValue" : 1,
"WhileStatementConditionalOpening" : 1,
"WhileStatementConditionalClosing" : 0,
"WhileStatementOpeningBrace" : 1,
"WhileStatementClosingBrace" : 1
},

"after" : {
"ArrayExpressionOpening" : 0,
"ArrayExpressionClosing" : 0,
"ArrayExpressionComma" : 1,
"ArgumentComma" : 1,
"ArgumentList" : 0,
"ArgumentListArrayExpression" : 0,
"ArgumentListFunctionExpression" : 0,
"ArgumentListObjectExpression" : 0,
"AssignmentOperator" : 1,
"BinaryExpression": 0,
"BinaryExpressionOperator" : 1,
"BlockComment" : 1,
"CallExpression" : 0,
"CatchParameterList" : 0,
"CatchOpeningBrace" : 1,
"CatchClosingBrace" : 1,
"CatchKeyword" : 1,
"CommaOperator" : 1,
"ConditionalExpressionConsequent" : 1,
"ConditionalExpressionTest" : 1,
"DoWhileStatementOpeningBrace" : 1,
"DoWhileStatementClosingBrace" : 1,
"DoWhileStatementBody" : 1,
"EmptyStatement" : 0,
"ExpressionOpeningParentheses" : 0,
"FinallyKeyword" : -1,
"FinallyOpeningBrace" : 1,
"FinallyClosingBrace" : 1,
"ForInStatement" : 1,
"ForInStatementExpressionOpening" : 0,
"ForInStatementExpressionClosing" : 1,
"ForInStatementOpeningBrace" : 1,
"ForInStatementClosingBrace" : 1,
"ForStatement" : 1,
"ForStatementExpressionOpening" : 0,
"ForStatementExpressionClosing" : 1,
"ForStatementClosingBrace" : 1,
"ForStatementOpeningBrace" : 1,
"ForStatementSemicolon" : 1,
"FunctionReservedWord": 0,
"FunctionName" : 0,
"FunctionExpressionOpeningBrace" : 1,
"FunctionExpressionClosingBrace" : 0,
"FunctionDeclarationOpeningBrace" : 0,
"FunctionDeclarationClosingBrace" : 0,
"IfStatementConditionalOpening" : 0,
"IfStatementConditionalClosing" : 1,
"IfStatementOpeningBrace" : 1,
"IfStatementClosingBrace" : 1,
"ElseStatementOpeningBrace" : 1,
"ElseStatementClosingBrace" : 1,
"ElseIfStatementOpeningBrace" : 1,
"ElseIfStatementClosingBrace" : 1,
"MemberExpressionOpening" : 0,
"LogicalExpressionOperator" : 1,
"ObjectExpressionClosingBrace": 0,
"PropertyName" : 0,
"PropertyValue" : 0,
"ParameterComma" : 1,
"ParameterList" : 0,
"SwitchDiscriminantOpening" : 0,
"SwitchDiscriminantClosing" : 1,
"ThrowKeyword": 1,
"TryKeyword": -1,
"TryOpeningBrace" : 1,
"TryClosingBrace" : 1,
"UnaryExpressionOperator": 0,
"VariableName" : 1,
"WhileStatementConditionalOpening" : 0,
"WhileStatementConditionalClosing" : 1,
"WhileStatementOpeningBrace" : 1,
"WhileStatementClosingBrace" : 1
}
}
}
19 changes: 19 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"eqeqeq": true,
"node": true,
"browser": true,
"newcap": true,
"predef": [
"describe",
"it",
"before",
"beforeEach",
"after",
"afterEach"
],
"unused": true,
"trailing": true,
"boss": true,
"loopfunc": true,
"undef": true
}
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.jshintrc
.jsfmtrc
.travis.yml
test/
examples/
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Lucas Hrabovsky <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# jsonpatch-to-mongodb

[![build status](https://secure.travis-ci.org/imlucas/jsonpatch-to-mongodb.png)](http://travis-ci.org/imlucas/jsonpatch-to-mongodb)

Convert [JSON patches](http://jsonpatch.com/) into a MongoDB update.

## Example

```javascript
var toMongodb = require('jsonpatch-to-mongodb');
var patches = [{
op: 'replace',
path: '/name',
value: 'dave'
}];

console.log(toMongodb(patches));
// {'$set': {name: 'dave'}};
```

Example: [with express and mongoose](tree/master/examples/express/index.js)


## Install

```
npm install --save jsonpatch-to-mongodb
```

## Test

```
npm test
```

## License

MIT
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var toDot = require('jsonpath-to-dot');

module.exports = function(patches){
var update = {};
patches.map(function(p){
if(p.op === 'add'){
if(!update.$push) update.$push = {};
update.$push[toDot(p.path)] = p.value;
}
else if(p.op === 'remove'){
if(!update.$unset) update.$unset = {};
update.$unset[toDot(p.path)] = 1;
}
else if(p.op === 'replace'){
if(!update.$set) update.$set = {};
update.$set[toDot(p.path)] = p.value;
}
});
return update;
};
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "jsonpatch-to-mongodb",
"version": "0.0.0",
"description": "Convert JSON patches into a MongoDB update",
"author": "Lucas Hrabovsky <[email protected]> (http://imlucas.com)",
"license": "MIT",
"homepage": "http://github.com/imlucas/jsonpatch-to-mongodb",
"repository": {
"type": "git",
"url": "git://github.com/imlucas/jsonpatch-to-mongodb.git"
},
"keywords": [
"mongodb.js",
"mongodb",
"jiff",
"jsonpatch",
"json patch mongodb"
],
"scripts": {
"test": "mocha"
},
"dependencies": {
"jsonpath-to-dot": "~1.0.0"
},
"devDependencies": {
"mocha": "~2.1.0"
}
}
20 changes: 20 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var assert = require('assert'),
toMongodb = require('../');

describe('jsonpatch to mongodb', function() {
it('should work', function() {
var patches = [{
op: 'replace',
path: '/name',
value: 'dave'
}];

var expected = {
$set: {
name: 'dave'
}
};

assert.deepEqual(toMongodb(patches), expected);
});
});
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-R spec

0 comments on commit b7576dc

Please sign in to comment.