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

[Fix,Deps] Reduce size by making dependency free and add tests without Weakmap and Map #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 2
trim_trailing_whitespace = true
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"rules": {
"max-lines-per-function": 0,
"multiline-comment-style": 1,
"new-cap": [2, { "capIsNewExceptions": ["GetIntrinsic"] }],
},
}
44 changes: 21 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
'use strict';

var GetIntrinsic = require('get-intrinsic');
Tofandel marked this conversation as resolved.
Show resolved Hide resolved
var callBound = require('call-bind/callBound');
var inspect = require('object-inspect');
var bind = require('function-bind');

var $TypeError = GetIntrinsic('%TypeError%');
var $WeakMap = GetIntrinsic('%WeakMap%', true);
var $Map = GetIntrinsic('%Map%', true);
var $call = Function.prototype.call;

var $weakMapGet = callBound('WeakMap.prototype.get', true);
var $weakMapSet = callBound('WeakMap.prototype.set', true);
var $weakMapHas = callBound('WeakMap.prototype.has', true);
var $mapGet = callBound('Map.prototype.get', true);
var $mapSet = callBound('Map.prototype.set', true);
var $mapHas = callBound('Map.prototype.has', true);
var callBound = function (original) {
return $call.call(bind, $call, original);
};
ljharb marked this conversation as resolved.
Show resolved Hide resolved

var $TypeError = TypeError;
var $WeakMap = typeof WeakMap === 'function' ? WeakMap : undefined;
var $Map = typeof Map === 'function' ? Map : undefined;

var $weakMapGet = $WeakMap ? callBound($WeakMap.prototype.get) : undefined;
var $weakMapSet = $WeakMap ? callBound($WeakMap.prototype.set) : undefined;
var $weakMapHas = $WeakMap ? callBound($WeakMap.prototype.has) : undefined;
var $mapGet = $Map ? callBound($Map.prototype.get) : undefined;
var $mapSet = $Map ? callBound($Map.prototype.set) : undefined;
var $mapHas = $Map ? callBound($Map.prototype.has) : undefined;

/*
* This function traverses the list returning the node corresponding to the
* given key.
*
* That node is also moved to the head of the list, so that if it's accessed
* again we don't need to traverse the whole list. By doing so, all the recently
* used nodes can be accessed relatively quickly.
*/
* This function traverses the list returning the node corresponding to the given key.
*
* That node is also moved to the head of the list, so that if it's accessed again we don't need to traverse the whole list. By doing so, all the recently used nodes can be accessed relatively quickly.
*/
var listGetNode = function (list, key) { // eslint-disable-line consistent-return
for (var prev = list, curr; (curr = prev.next) !== null; prev = curr) {
if (curr.key === key) {
Expand Down Expand Up @@ -109,11 +111,7 @@ module.exports = function getSideChannel() {
$mapSet($m, key, value);
} else {
if (!$o) {
/*
* Initialize the linked list as an empty node, so that we don't have
* to special-case handling of the first node: we can always refer to
* it as (previous node).next, instead of something like (list).head
*/
// Initialize the linked list as an empty node, so that we don't have to special-case handling of the first node: we can always refer to it as (previous node).next, instead of something like (list).head
$o = { key: {}, next: null };
}
listSet($o, key, value);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"scripts": {
"prepublishOnly": "safe-publish-latest",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prelint": "eclint check $(git ls-files | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git')",
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint",
"tests-only": "nyc tape 'test/**/*.js'",
Expand Down Expand Up @@ -47,14 +48,14 @@
"@ljharb/eslint-config": "^20.2.3",
"aud": "^2.0.0",
"auto-changelog": "^2.4.0",
"eclint": "^2.8.1",
"eslint": "=8.8.0",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.5.2"
},
"dependencies": {
"call-bind": "^1.0.2",
"get-intrinsic": "^1.1.1",
"function-bind": "^1.1.1",
"object-inspect": "^1.12.0"
},
"auto-changelog": {
Expand Down
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ test('has', function (t) {
channel.set(o, 'foo');
t.equal(channel.has(o), true, 'existent value yields true');

t.equal(channel.has('abc'), false, 'non object value non existent yields false');

channel.set('abc', 'foo');
t.equal(channel.has('abc'), true, 'non object value that exists yields true');

t.end();
});

Expand Down