lopos
v0.0.3
Published
Lopos is like a clone of if() but with more options, also there is a test command allows you to test anything ugh idk.
Readme
lopos
Lopos is a powerful utility library for advanced conditional testing, flexible date handling, deep merging, throttling, cloning, and conditional logic — all packed with extensive options and without relying on native if statements in some parts.
Ideal for developers who want flexible, readable, and highly customizable utility functions in JavaScript.
Website
Installation
npm install loposImporting
const {
test,
lopif,
deepMerge,
flexibleDate,
advancedThrottle,
deepClone
} = require("lopos");Functions
test(type, actual, expected, options)
Flexible test function for many types of comparisons and validations, including deep equality, string checks, array contents, regex, async functions, and custom predicates.
- type: String key defining the test type.
- actual: Value to test.
- expected: Value or parameter used in the test.
- options: Optional flags such as
ignoreCase,tolerance, ordeepfor customizing behavior. - returns:
booleanresult of the test.
Example:
test("equals", 5, 5); // true
test("contains", "Hello World", "world", { ignoreCase: true }); // true
test("inRange", 10, [5, 15]); // truelopif(condition, onTrue, onFalse, options)
Conditional execution mimicking if-else logic without using if statements. Supports chained conditions, hooks before and after execution, and flexible handlers.
- condition: Boolean or truthy/falsy value.
- onTrue/onFalse: Values or functions executed based on condition.
- options: Optional hooks (
pre,post) and chaining of multiple conditionals. - returns: Result of executed handler or fallback.
Example:
lopif(
10 > 5,
() => "Yes",
() => "No",
{ pre: cond => console.log("Checking:", cond), post: res => console.log("Result:", res) }
);
// Logs: Checking: true
// Logs: Result: Yes
// Returns: "Yes"deepMerge(...sources, options)
Deeply merges multiple objects or arrays with customizable behavior such as array concatenation, cloning, and custom merge strategies.
- sources: Two or more objects or arrays.
- options: Controls like
arrayConcat,clone, andcustomizerfunction. - returns: A new deeply merged object or array.
Example:
deepMerge(
{ a: 1, b: { c: 2 } },
{ b: { d: 3 }, e: 4 },
{ arrayConcat: false }
);
// Result: { a: 1, b: { c: 2, d: 3 }, e: 4 }flexibleDate(input, options)
Parse, validate, adjust, and format dates flexibly. Supports strings, timestamps, Date objects, relative time, timezone offsets, strict mode, and custom output formatting.
- input: Date input (string, number, Date, null).
- options: Settings like
format,strict,fallback,timezone,relative,adjustDays. - returns: Formatted date string, timestamp, Date object, or fallback.
Example:
flexibleDate("2023-01-01T12:00:00Z", { format: "locale", timezone: "utc" });
// Might return: "1/1/2023, 12:00:00 PM"
flexibleDate(null, { fallback: "Invalid Date" });
// Returns: "Invalid Date"advancedThrottle(func, delay, options)
Throttle a function with advanced controls like leading/trailing call, max call limits, cancellation, and flushing.
- func: Function to throttle.
- delay: Delay in milliseconds.
- options:
leading,trailing,maxCalls. - returns: Throttled function with
cancelandflushmethods.
Example:
const throttled = advancedThrottle(() => console.log("Called!"), 1000, { leading: true });
throttled();
throttled.cancel();
throttled.flush();deepClone(value)
Deeply clones any value including complex types like objects, arrays, maps, sets, dates, regexps, with support for circular references.
- value: Any JavaScript value to clone.
- returns: Deep cloned copy of the value.
Example:
const obj = { a: 1, b: { c: 2 } };
const clone = deepClone(obj);
clone.b.c = 3;
console.log(obj.b.c); // 2 (original unchanged)Made with ❤️ for developers who want advanced, clean, and expressive utility functions in JavaScript.
Feel free to contribute or open issues!
