Skip to content

Commit a0e56df

Browse files
committed
First implementation
1 parent 007385e commit a0e56df

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function addType(type)
2+
{
3+
var item = this[type]
4+
item.type = type
5+
6+
return item
7+
}
8+
9+
10+
function forceObjectsArray(array)
11+
{
12+
// null / undefined
13+
if(array == null) return []
14+
15+
// Array
16+
if(array instanceof Array) return array
17+
18+
// Single object
19+
if(array.type) return [array]
20+
21+
// Mapping / literal object
22+
if(array.constuctor.name === 'Object') return array.keys().map(addType, array)
23+
24+
// Invalid object
25+
var error = new SyntaxError('Invalid object')
26+
error.data = array
27+
28+
throw error
29+
}
30+
31+
32+
module.exports = forceObjectsArray

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "force-objects-array",
3+
"version": "0.0.0",
4+
"description": "Get an array of objects when giving a single object element or a mapping",
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/piranna/force-objects-array.git"
12+
},
13+
"keywords": [
14+
"array",
15+
"object",
16+
"mapping",
17+
"convert"
18+
],
19+
"author": "Telefónica I+D",
20+
"contributors": [
21+
"Jesús Leganés Combarro 'piranna' <[email protected]>"
22+
],
23+
"license": "AGPL-3.0",
24+
"bugs": {
25+
"url": "https://github.com/piranna/force-objects-array/issues"
26+
},
27+
"homepage": "https://github.com/piranna/force-objects-array#readme"
28+
}

0 commit comments

Comments
 (0)