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.
- Wildcard Search: Supports flexible pattern matching.
- Cross-Context Support: Works with objects, JSON, and the DOM.
- Lightweight: Minimal dependencies for faster performance.
Install the package via npm:
npm install --save-dev https://github.com/JustinLawrenceMS/somewhat
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" } ]
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" } ]
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" } ]
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" } ]
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!" } ]
Run the tests using Jest:
npm test
Contributions are welcome! Please fork the repository and submit a pull request.
If you encounter issues, please open an issue on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.