Skip to content

Commit 3dfcf58

Browse files
committed
Helper for storing objects in MongoDB
0 parents  commit 3dfcf58

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Components #
55+
##############
56+
57+
/build
58+
/components
59+
/vendors

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.js

component.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "mongodb-ok-for-storage",
3+
"description": "Helper for storing objects in MongoDB",
4+
"version": "0.0.1",
5+
"main": "index.js",
6+
"scripts": [
7+
"index.js"
8+
]
9+
}

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
exports.is = function isOk(obj) {
2+
return !Object.keys(obj).some(function (key) {
3+
var value = obj[key]
4+
5+
if (key[0] === '$' || key.match(/\./)) return true;
6+
if (Object(value) === value) return !isOk(value);
7+
8+
return false
9+
})
10+
}
11+
12+
exports.remove = function remove(obj) {
13+
var buf = {}
14+
15+
Object.keys(obj).forEach(function (key) {
16+
var value = obj[key]
17+
18+
if (key[0] !== '$' && !key.match(/\./))
19+
buf[key] = Object(value) === value ? remove(value) : value;
20+
})
21+
22+
return buf
23+
}

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "mongodb-ok-for-storage",
3+
"description": "Helper for storing objects in MongoDB",
4+
"version": "0.0.1",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/discore/mongodb-ok-for-storage.git"
12+
},
13+
"bugs": {
14+
"mail": "[email protected]",
15+
"url": "https://github.com/discore/mongodb-ok-for-storage/issues"
16+
}
17+
}

test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var assert = require('assert')
2+
3+
var okForStorage = require('./')
4+
5+
assert.ok(!okForStorage.is({
6+
'a.b': 1
7+
}))
8+
9+
assert.ok(okForStorage.is({
10+
'a': 1
11+
}))
12+
13+
assert.ok(!okForStorage.is({
14+
a: {
15+
'a.b': 1
16+
}
17+
}))
18+
19+
assert.ok(!okForStorage.is({
20+
a: {
21+
$b: 1
22+
}
23+
}))
24+
25+
var obj1 = okForStorage.remove({
26+
a: {
27+
'a.b': 1,
28+
$set: true
29+
}
30+
})
31+
32+
assert.ok(!obj1.a['a.b'])
33+
assert.ok(!obj1.a.$set)

0 commit comments

Comments
 (0)