Skip to content

Commit eddab86

Browse files
authored
feat!: parameter object is null object (#333)
* feat: parameter object is null object * exclude windows and node 14
1 parent ea27fa2 commit eddab86

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

lib/handler-storage.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { NullObject } = require('./null-object')
34
const httpMethodStrategy = require('./strategies/http-method')
45

56
class HandlerStorage {
@@ -61,11 +62,20 @@ class HandlerStorage {
6162
}
6263

6364
_compileCreateParamsObject (params) {
64-
const lines = []
65+
const fnBody = []
66+
67+
fnBody.push('const fn = function _createParamsObject (paramsArray) {')
68+
69+
fnBody.push('const params = new NullObject()')
6570
for (let i = 0; i < params.length; i++) {
66-
lines.push(`'${params[i]}': paramsArray[${i}]`)
71+
fnBody.push(`params['${params[i]}'] = paramsArray[${i}]`)
6772
}
68-
return new Function('paramsArray', `return {${lines.join(',')}}`) // eslint-disable-line
73+
fnBody.push('return params')
74+
fnBody.push('}')
75+
76+
fnBody.push('return fn')
77+
78+
return new Function('NullObject', fnBody.join('\n'))(NullObject) // eslint-disable-line
6979
}
7080

7181
_getHandlerMatchingConstraints () {

lib/null-object.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
const NullObject = function () {}
4+
NullObject.prototype = Object.create(null)
5+
6+
module.exports = {
7+
NullObject
8+
}

test/null-object.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { NullObject } = require('../lib/null-object')
5+
6+
test('NullObject', t => {
7+
t.plan(2)
8+
const nullObject = new NullObject()
9+
t.ok(nullObject instanceof NullObject)
10+
t.ok(typeof nullObject === 'object')
11+
})
12+
13+
test('has no methods from generic Object class', t => {
14+
function getAllPropertyNames (obj) {
15+
var props = []
16+
17+
do {
18+
Object.getOwnPropertyNames(obj).forEach(function (prop) {
19+
if (props.indexOf(prop) === -1) {
20+
props.push(prop)
21+
}
22+
})
23+
} while (obj = Object.getPrototypeOf(obj)) // eslint-disable-line
24+
25+
return props
26+
}
27+
const propertyNames = getAllPropertyNames({})
28+
t.plan(propertyNames.length + 1)
29+
30+
const nullObject = new NullObject()
31+
32+
for (const propertyName of propertyNames) {
33+
t.notOk(propertyName in nullObject, propertyName)
34+
}
35+
t.equal(getAllPropertyNames(nullObject).length, 0)
36+
})

0 commit comments

Comments
 (0)