Skip to content

Commit

Permalink
feat!: parameter object is null object (#333)
Browse files Browse the repository at this point in the history
* feat: parameter object is null object

* exclude windows and node 14
  • Loading branch information
Uzlopak committed Sep 2, 2024
1 parent ea27fa2 commit eddab86
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/handler-storage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

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

class HandlerStorage {
Expand Down Expand Up @@ -61,11 +62,20 @@ class HandlerStorage {
}

_compileCreateParamsObject (params) {
const lines = []
const fnBody = []

fnBody.push('const fn = function _createParamsObject (paramsArray) {')

fnBody.push('const params = new NullObject()')
for (let i = 0; i < params.length; i++) {
lines.push(`'${params[i]}': paramsArray[${i}]`)
fnBody.push(`params['${params[i]}'] = paramsArray[${i}]`)
}
return new Function('paramsArray', `return {${lines.join(',')}}`) // eslint-disable-line
fnBody.push('return params')
fnBody.push('}')

fnBody.push('return fn')

return new Function('NullObject', fnBody.join('\n'))(NullObject) // eslint-disable-line
}

_getHandlerMatchingConstraints () {
Expand Down
8 changes: 8 additions & 0 deletions lib/null-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const NullObject = function () {}
NullObject.prototype = Object.create(null)

module.exports = {
NullObject
}
36 changes: 36 additions & 0 deletions test/null-object.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const { test } = require('tap')
const { NullObject } = require('../lib/null-object')

test('NullObject', t => {
t.plan(2)
const nullObject = new NullObject()
t.ok(nullObject instanceof NullObject)
t.ok(typeof nullObject === 'object')
})

test('has no methods from generic Object class', t => {
function getAllPropertyNames (obj) {
var props = []

do {
Object.getOwnPropertyNames(obj).forEach(function (prop) {
if (props.indexOf(prop) === -1) {
props.push(prop)
}
})
} while (obj = Object.getPrototypeOf(obj)) // eslint-disable-line

return props
}
const propertyNames = getAllPropertyNames({})
t.plan(propertyNames.length + 1)

const nullObject = new NullObject()

for (const propertyName of propertyNames) {
t.notOk(propertyName in nullObject, propertyName)
}
t.equal(getAllPropertyNames(nullObject).length, 0)
})

0 comments on commit eddab86

Please sign in to comment.