@technobuddha/library
Version: 
A large library of useful functions
41 lines • 1.78 kB
JavaScript
/**
 * Determines whether two arrays of dependencies have changed (shallow comparison).
 *
 * Similar to how React compares dependencies in hooks.
 * Returns true if the arrays are different in length or any element is not strictly equal (===).
 *
 * @param a - The previous dependency array.
 * @param b - The next dependency array.
 * @returns `true` if the arrays differ in length or any element, otherwise `false`.
 *
 * @example
 * ```typescript
 * // Basic usage
 * changed([1, 2, 3], [1, 2, 3]); // false
 * changed([1, 2, 3], [1, 2, 4]); // true
 * changed([1, 2], [1, 2, 3]);    // true
 * changed([], []);               // false
 * ```
 *
 * @example
 * ```typescript
 * // In a custom hook
 * function useCustom(dep1, dep2) {
 *   const deps = [dep1, dep2];
 *   const prevDeps = React.useRef(deps);
 *   React.useEffect(() => {
 *     if (changed(prevDeps.current, deps)) {
 *       // dependencies changed, do something
 *       prevDeps.current = deps;
 *     }
 *   }, [dep1, dep2]);
 * }
 * ```
 * @group Array
 * @category Comparison
 */
export function changed(a, b) {
    return (a.length !== b.length ||
        !a.reduce((acc, value, index) => acc && b[index] === value, true));
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hhbmdlZC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9jaGFuZ2VkLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW1DRztBQUNILE1BQU0sVUFBVSxPQUFPLENBQUMsQ0FBcUIsRUFBRSxDQUFxQjtJQUNsRSxPQUFPLENBQ0wsQ0FBQyxDQUFDLE1BQU0sS0FBSyxDQUFDLENBQUMsTUFBTTtRQUNyQixDQUFDLENBQUMsQ0FBQyxNQUFNLENBQVUsQ0FBQyxHQUFHLEVBQUUsS0FBSyxFQUFFLEtBQUssRUFBRSxFQUFFLENBQUMsR0FBRyxJQUFJLENBQUMsQ0FBQyxLQUFLLENBQUMsS0FBSyxLQUFLLEVBQUUsSUFBSSxDQUFDLENBQzNFLENBQUM7QUFDSixDQUFDIn0=