ast-delete-object1.9.2

Delete all plain objects in AST if they contain a certain key/value pair

§ Quick Take

import { strict as assert } from "assert";
import deleteObj from "ast-delete-object";

// if all keys in source object match target object's keys, the
// source object gets deleted:
assert.deepEqual(
  deleteObj(
    [
      "elem1",
      {
        findme1: "zzz",
        findme2: "yyy",
        somethingelse: "qqq",
      },
      "elem2",
    ],
    {
      findme1: "zzz",
      findme2: "yyy",
    }
  ),
  ["elem1", "elem2"]
);

§ Examples

§ Purpose

It deletes objects in AST if all given keys-value pairs are matched.

§ API

deleteObj(input, objToDelete, [options])

In other words, it's a function which takes three input arguments, third-one being optional (marked by square brackets). It will not mutate the input AST.

§ API - Input

Input argumentTypeObligatory?Description
inputWhateveryesAST tree, or object or array or whatever. Can be deeply-nested.
objToDeleteWhateveryesKey/value pairs that should be used to match plain objects.
optionsBooleannoOOO: Optional Options Object (see API below)

By the way, the input arguments are not mutated in any way.

§ API - Options object

options object's keyTypeObligatory?DefaultDescription
matchKeysStrictlyBooleannofalseIf you supplied an object to match, and all its keys were found in target object, that target object will be deleted. Now, there could have been extra keys there. If you set matchKeysStrictly to true, both keysets as well as key values have to match.
hungryForWhitespaceBooleannofalseWhen active, empty value (one which would get trim-med to empty string, "") will match any other empty value (which might be different matching strictly, yet trim to the same empty string, "").

Defaults in one place:

{
matchKeysStrictly: false,
hungryForWhitespace: false,
}

§ API - Output

This library will return the clone of first input argument with relevant elements deleted.

§ opts.matchKeysStrictly

If you want the search to be strict, that is to require the key set to match exactly, use options object, matchKeysStrictly: true:

const deleteObj = require("ast-delete-object");
let res = deleteObj(
[
"elem1",
{
findme1: "zzz",
findme2: "yyy",
somethingelse: "qqq", // <--- this key will block deletion
},
"elem2",
],
{
findme1: "zzz",
findme2: "yyy",
},
{
matchKeysStrictly: true, // <--- strict matching
}
);
console.log("res = " + JSON.stringify(res, null, 4));
// => nothing changes!
// [
// 'elem1',
// {
// findme1: 'zzz',
// findme2: 'yyy',
// somethingelse: 'qqq'
// },
// 'elem2'
// ]

In example above, object was not deleted because strict matching ignored it because of unrecognised key somethingelse.

§ opts.hungryForWhitespace

This is a library to deal with AST's, and they usually have lots of white space. Often there are many elements that contains only spaces, tabs or line breaks. Sometimes we want to pretend that those elements containing white space don't exist, so deletion is more aggressive regarding the white space.

For example, notice how we look for blank plain object, but it catches other objects that contain only empty space:

const deleteObj = require("ast-delete-object");
let res = deleteObj(
[
{ a: "\n" },
{
key3: "val3",
key4: "val4",
},
{ b: " " },
{ c: "" },
],
{},
{ matchKeysStrictly: false, hungryForWhitespace: true }
);
console.log("res = " + JSON.stringify(res, null, 4));
// => [{
// key3: 'val3',
// key4: 'val4'
// }]

§ The story

Originally, this program was created to help us delete unused CSS (as objects) from parsed HTML (AST tree) in email-comb. Since then, we've rewritten email-comb to process the code without parsing, without AST. email-comb became faster by magnitude (milliseconds instead of seconds/minutes).

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 email-comb 3.10.4
Remove unused CSS from email templates
📦 ast-monkey-util 1.1.11
Utility library of AST helper functions
📦 ast-get-values-by-key 2.7.0
Read or edit parsed HTML (or AST in general)
📦 ast-loose-compare 1.8.16
Compare anything: AST, objects, arrays and strings
📦 ast-get-object 1.9.18
Getter/setter for nested parsed HTML AST's, querying objects by key/value pairs
📦 ast-compare 1.13.19
Compare anything: AST, objects, arrays, strings and nested thereof
📦 ast-deep-contains 1.1.21
Like t.same assert on array of objects, where element order doesn't matter.