From 16d40a36da12a4d177f7124655c2417bfa7cfa19 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 29 Aug 2023 00:27:04 +0200 Subject: [PATCH 1/2] feat: parameter object is null object --- lib/handler-storage.js | 16 +++++++++++++--- lib/null-object.js | 8 ++++++++ test/null-object.test.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 lib/null-object.js create mode 100644 test/null-object.test.js diff --git a/lib/handler-storage.js b/lib/handler-storage.js index 783507e..9a028f5 100644 --- a/lib/handler-storage.js +++ b/lib/handler-storage.js @@ -1,5 +1,6 @@ 'use strict' +const { NullObject } = require('./null-object') const httpMethodStrategy = require('./strategies/http-method') class HandlerStorage { @@ -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 () { diff --git a/lib/null-object.js b/lib/null-object.js new file mode 100644 index 0000000..0740f04 --- /dev/null +++ b/lib/null-object.js @@ -0,0 +1,8 @@ +'use strict' + +const NullObject = function () {} +NullObject.prototype = Object.create(null) + +module.exports = { + NullObject +} diff --git a/test/null-object.test.js b/test/null-object.test.js new file mode 100644 index 0000000..57a9471 --- /dev/null +++ b/test/null-object.test.js @@ -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) +}) From 869abe52af546652a0860f653cd7c98c17967996 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 14 Sep 2023 09:58:26 +0200 Subject: [PATCH 2/2] exclude windows and node 14 --- .github/workflows/node.js.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 073e093..25f2f4f 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -12,8 +12,11 @@ jobs: strategy: matrix: - node-version: [14.x, 16.x, 18.x] + node-version: [14, 16, 18] os: [ubuntu-latest, windows-latest, macOS-latest] + exclude: + - node-version: 14 + os: windows-latest steps: - uses: actions/checkout@v3