Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: parameter object is null object #333

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
Loading