Skip to content

Latest commit

 

History

History
111 lines (74 loc) · 2.58 KB

README.md

File metadata and controls

111 lines (74 loc) · 2.58 KB

Somewhat

Somewhat is a lightweight utility package that simplifies wildcard searches in JavaScript objects, DOM elements, and JSON. This package is ideal for developers who need quick and effective searching capabilities in their applications.

npm version MIT License

Features

  • Wildcard Search: Supports flexible pattern matching.
  • Cross-Context Support: Works with objects, JSON, and the DOM.
  • Lightweight: Minimal dependencies for faster performance.

Installation

Install the package via npm:

npm install --save-dev https://github.com/JustinLawrenceMS/somewhat 

Quick Start

import Somewhat from "somewhat";

const obj = { a: "apple", b: "banana" };
const searcher = new Somewhat();
const results = searcher.searchObject(obj, "a*");

console.log(results); // Output: [ { path: "a", value: "apple" } ]

Usage

Searching Objects

You can use Somewhat to search JavaScript objects:

const obj = {
  a: "apple",
  b: "banana",
  c: "cherry",
};

const results = searcher.searchObject(obj, "a*");
console.log(results);
// Output: [ { path: "a", value: "apple" } ]

Searching the DOM

Search the DOM for elements matching a wildcard pattern:

const results = searcher.searchDOM(document.body, "data-*");
console.log(results);
// Output: [ { path: ".children[0].children[1]@data-attr", value: "test" } ]

Searching JSON

You can search JSON data using the same method as objects:

const jsonData = {
  a: "apple",
  b: "banana",
  nested: {
    c: "cherry",
  },
};

const results = searcher.searchObject(jsonData, "*erry");
console.log(results);
// Output: [ { path: "nested.c", value: "cherry" } ]

Searching the Entire Document

Search the entire document for text content matching a pattern:

const results = searcher.searchDocument("*world*");
console.log(results);
// Output: [ { path: ".children[0].textContent", value: "Hello, world!" } ]

Testing

Run the tests using Jest:

npm test

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

Support

If you encounter issues, please open an issue on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Donate

Donate