🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

@jridgewell/set-array

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jridgewell/set-array - npm Package Compare versions

Comparing version

to
1.2.0

87

dist/set-array.umd.js

@@ -8,15 +8,2 @@ (function (global, factory) {

/**
* Gets the index associated with `key` in the backing array, if it is already present.
*/
exports.get = void 0;
/**
* Puts `key` into the backing array, if it is not already present. Returns
* the index of the `key` in the backing array.
*/
exports.put = void 0;
/**
* Pops the last added item out of the SetArray.
*/
exports.pop = void 0;
/**
* SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the

@@ -35,22 +22,60 @@ * index of the `key` in the backing array.

}
(() => {
exports.get = (strarr, key) => strarr._indexes[key];
exports.put = (strarr, key) => {
// The key may or may not be present. If it is present, it's a number.
const index = exports.get(strarr, key);
if (index !== undefined)
return index;
const { array, _indexes: indexes } = strarr;
return (indexes[key] = array.push(key) - 1);
};
exports.pop = (strarr) => {
const { array, _indexes: indexes } = strarr;
if (array.length === 0)
return;
const last = array.pop();
indexes[last] = undefined;
};
})();
/**
* Typescript doesn't allow friend access to private fields, so this just casts the set into a type
* with public access modifiers.
*/
function cast(set) {
return set;
}
/**
* Gets the index associated with `key` in the backing array, if it is already present.
*/
function get(setarr, key) {
return cast(setarr)._indexes[key];
}
/**
* Puts `key` into the backing array, if it is not already present. Returns
* the index of the `key` in the backing array.
*/
function put(setarr, key) {
// The key may or may not be present. If it is present, it's a number.
const index = get(setarr, key);
if (index !== undefined)
return index;
const { array, _indexes: indexes } = cast(setarr);
const length = array.push(key);
return (indexes[key] = length - 1);
}
/**
* Pops the last added item out of the SetArray.
*/
function pop(setarr) {
const { array, _indexes: indexes } = cast(setarr);
if (array.length === 0)
return;
const last = array.pop();
indexes[last] = undefined;
}
/**
* Removes the key, if it exists in the set.
*/
function remove(setarr, key) {
const index = get(setarr, key);
if (index === undefined)
return;
const { array, _indexes: indexes } = cast(setarr);
for (let i = index + 1; i < array.length; i++) {
const k = array[i];
array[i - 1] = k;
indexes[k]--;
}
indexes[key] = undefined;
array.pop();
}
exports.SetArray = SetArray;
exports.get = get;
exports.pop = pop;
exports.put = put;
exports.remove = remove;

@@ -57,0 +82,0 @@ Object.defineProperty(exports, '__esModule', { value: true });

@@ -0,5 +1,19 @@

declare type Key = string | number | symbol;
/**
* SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
* index of the `key` in the backing array.
*
* This is designed to allow synchronizing a second array with the contents of the backing array,
* like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
* and there are never duplicates.
*/
export declare class SetArray<T extends Key> {
private _indexes;
array: readonly T[];
constructor();
}
/**
* Gets the index associated with `key` in the backing array, if it is already present.
*/
export declare let get: (strarr: SetArray, key: string) => number | undefined;
export declare function get<T extends Key>(setarr: SetArray<T>, key: T): number | undefined;
/**

@@ -9,19 +23,11 @@ * Puts `key` into the backing array, if it is not already present. Returns

*/
export declare let put: (strarr: SetArray, key: string) => number;
export declare function put<T extends Key>(setarr: SetArray<T>, key: T): number;
/**
* Pops the last added item out of the SetArray.
*/
export declare let pop: (strarr: SetArray) => void;
export declare function pop<T extends Key>(setarr: SetArray<T>): void;
/**
* SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
* index of the `key` in the backing array.
*
* This is designed to allow synchronizing a second array with the contents of the backing array,
* like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
* and there are never duplicates.
* Removes the key, if it exists in the set.
*/
export declare class SetArray {
private _indexes;
array: readonly string[];
constructor();
}
export declare function remove<T extends Key>(setarr: SetArray<T>, key: T): void;
export {};
{
"name": "@jridgewell/set-array",
"version": "1.1.2",
"version": "1.2.0",
"description": "Like a Set, but provides the index of the `key` in the backing array",

@@ -25,4 +25,3 @@ "keywords": [],

"files": [
"dist",
"src"
"dist"
],

@@ -40,3 +39,2 @@ "engines": {

"lint:ts": "npm run test:lint:ts -- --fix",
"pretest": "run-s build:rollup",
"test": "run-s -n test:lint test:only",

@@ -66,4 +64,5 @@ "test:debug": "mocha --inspect-brk",

"rollup": "2.66.0",
"tsx": "4.7.1",
"typescript": "4.5.5"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet