Static utility methods available on the Cash function for type checking, iteration, object manipulation, and HTML parsing.
Utility functions to check JavaScript data types with cross-browser compatibility.
/**
* Check if value is an array
* @param x - Value to test
* @returns True if value is an array
*/
isArray(x: any): x is Array<any>;
/**
* Check if value is a function
* @param x - Value to test
* @returns True if value is a function
*/
isFunction(x: any): x is Function;
/**
* Check if value is numeric
* @param x - Value to test
* @returns True if value can be treated as a number
*/
isNumeric(x: any): boolean;
/**
* Check if value is a Window object
* @param x - Value to test
* @returns True if value is a Window object
*/
isWindow(x: any): x is Window;Usage Examples:
// Array checking
$.isArray([1, 2, 3]); // true
$.isArray('string'); // false
$.isArray({ length: 3 }); // false (array-like but not array)
// Function checking
$.isFunction(function() {}); // true
$.isFunction(() => {}); // true
$.isFunction('string'); // false
$.isFunction(Math.max); // true
// Numeric checking
$.isNumeric(42); // true
$.isNumeric('42'); // true
$.isNumeric('42.5'); // true
$.isNumeric('42px'); // false
$.isNumeric('abc'); // false
$.isNumeric(NaN); // false
$.isNumeric(Infinity); // true
// Window checking
$.isWindow(window); // true
$.isWindow(document); // false
$.isWindow(frames[0]); // true (if frame exists)
// Practical usage in functions
function processValue(value) {
if ($.isArray(value)) {
return value.map(item => processValue(item));
} else if ($.isNumeric(value)) {
return parseFloat(value);
} else if ($.isFunction(value)) {
return value();
}
return value;
}
// Type guards for different handling
function handleData(data) {
if ($.isArray(data)) {
data.forEach(item => processItem(item));
} else {
processItem(data);
}
}Iterate over arrays and array-like objects with jQuery-compatible semantics.
/**
* Iterate over array-like objects
* @param arr - Array or array-like object to iterate
* @param callback - Function called for each item (index, item)
* @returns The original array
*/
each<T>(arr: ArrayLike<T>, callback: EachCallback<T>): void;Usage Examples:
// Array iteration
$.each([1, 2, 3], function(index, value) {
console.log(`Index: ${index}, Value: ${value}`);
});
// Object iteration (treating as array-like)
const nodeList = document.querySelectorAll('div');
$.each(nodeList, function(index, element) {
element.classList.add('processed');
});
// Early termination by returning false
$.each(['a', 'b', 'c', 'd'], function(index, letter) {
if (letter === 'c') {
return false; // Stop iteration
}
console.log(letter); // Only logs 'a' and 'b'
});
// Processing arrays of objects
const users = [{ name: 'John' }, { name: 'Jane' }];
$.each(users, function(index, user) {
user.id = index + 1;
user.processed = true;
});
// Working with jQuery-like collections
$.each($('.items'), function(index, element) {
$(element).data('order', index);
});Extend objects with properties from other objects, with deep and shallow merge support.
/**
* Extend with no arguments (returns empty object)
* @returns Empty object
*/
extend(): any;
/**
* Extend the Cash function itself
* @param target - Properties to add to Cash
* @returns The Cash function
*/
extend(target: any): typeof cash;
/**
* Extend target object with source objects
* @param target - Object to extend
* @param objs - Source objects to copy properties from
* @returns Extended target object
*/
extend(target: any, ...objs: any[]): any;Usage Examples:
// Basic object extension
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const result = $.extend(obj1, obj2);
// result === obj1 === { a: 1, b: 3, c: 4 }
// Multiple source objects
const target = {};
$.extend(target, { a: 1 }, { b: 2 }, { c: 3 });
// target === { a: 1, b: 2, c: 3 }
// Configuration merging
const defaultConfig = {
timeout: 5000,
retries: 3,
cache: true
};
const userConfig = {
timeout: 10000,
debug: true
};
const config = $.extend({}, defaultConfig, userConfig);
// config === { timeout: 10000, retries: 3, cache: true, debug: true }
// Extending Cash itself (adding plugins)
$.extend({
myPlugin: function(options) {
return this.each(function() {
// Plugin implementation
});
}
});
// Now available as $('.elements').myPlugin();
// Deep cloning (manual deep extend)
function deepExtend(target, ...sources) {
sources.forEach(source => {
Object.keys(source).forEach(key => {
if (source[key] && typeof source[key] === 'object' && !$.isArray(source[key])) {
target[key] = target[key] || {};
deepExtend(target[key], source[key]);
} else {
target[key] = source[key];
}
});
});
return target;
}Utilities for working with arrays and removing duplicates.
/**
* Remove duplicate values from array-like object
* @param arr - Array-like object to process
* @returns Array-like object with duplicates removed
*/
unique<T>(arr: ArrayLike<T>): ArrayLike<T>;Usage Examples:
// Remove duplicates from arrays
const withDupes = [1, 2, 2, 3, 3, 3, 4];
const unique = $.unique(withDupes);
// unique === [1, 2, 3, 4]
// Works with DOM elements
const elements = $('.class1, .class2, .class1'); // May have duplicates
const uniqueElements = $.unique(elements);
// String arrays
const tags = ['red', 'blue', 'red', 'green', 'blue'];
const uniqueTags = $.unique(tags);
// uniqueTags === ['red', 'blue', 'green']
// Working with NodeLists
const nodeList = document.querySelectorAll('div, span, div');
const uniqueNodes = $.unique(nodeList);
// Combining arrays and removing duplicates
const arr1 = [1, 2, 3];
const arr2 = [3, 4, 5];
const combined = $.unique(arr1.concat(arr2));
// combined === [1, 2, 3, 4, 5]Parse HTML strings into DOM elements.
/**
* Parse HTML string into DOM elements
* @param html - HTML string to parse
* @returns Array of DOM elements
*/
parseHTML(html: string): EleLoose[];Usage Examples:
// Parse simple HTML
const elements = $.parseHTML('<div>Hello</div><span>World</span>');
// elements === [div element, span element]
// Parse complex HTML
const complexHTML = `
<div class="card">
<h3>Title</h3>
<p>Content here</p>
<button>Action</button>
</div>
`;
const cardElements = $.parseHTML(complexHTML);
// Use parsed elements
$.each(cardElements, function(index, element) {
document.body.appendChild(element);
});
// Create elements programmatically
function createCard(title, content) {
const html = `
<div class="card">
<h3>${title}</h3>
<p>${content}</p>
</div>
`;
return $.parseHTML(html)[0]; // Return first (and only) element
}
// Template processing
const template = '<li data-id="{{id}}">{{name}}</li>';
function createListItem(data) {
const html = template.replace('{{id}}', data.id).replace('{{name}}', data.name);
return $.parseHTML(html)[0];
}
// Batch element creation
const items = ['Item 1', 'Item 2', 'Item 3'];
const listItems = items.map(item => {
return $.parseHTML(`<li>${item}</li>`)[0];
});Global properties available on the Cash function.
/**
* Global unique identifier counter
*/
guid: number;
/**
* Reference to Cash prototype for extending
*/
fn: Cash;Usage Examples:
// Generate unique IDs
function generateId() {
return 'element-' + (++$.guid);
}
const id1 = generateId(); // 'element-1'
const id2 = generateId(); // 'element-2'
// Add methods to all Cash collections
$.fn.myMethod = function() {
return this.each(function() {
// Method implementation
});
};
// Now available on all collections
$('.elements').myMethod();
// Plugin pattern
$.fn.highlighter = function(options) {
const settings = $.extend({
color: 'yellow',
duration: 1000
}, options);
return this.each(function() {
$(this).css('background-color', settings.color);
setTimeout(() => {
$(this).css('background-color', '');
}, settings.duration);
});
};
// Usage: $('.text').highlighter({ color: 'green' });function safeProcess(data) {
if ($.isArray(data)) {
return data.filter(item => $.isNumeric(item)).map(Number);
} else if ($.isNumeric(data)) {
return [Number(data)];
} else if ($.isFunction(data)) {
return safeProcess(data());
}
return [];
}// Full plugin pattern
(function($) {
$.fn.accordion = function(options) {
const settings = $.extend({
activeClass: 'active',
speed: 300
}, options);
return this.each(function() {
const accordion = $(this);
const headers = accordion.find('.accordion-header');
headers.on('click', function() {
const header = $(this);
const content = header.next('.accordion-content');
const isActive = header.hasClass(settings.activeClass);
// Close all sections
headers.removeClass(settings.activeClass);
accordion.find('.accordion-content').hide();
// Open clicked section if it wasn't active
if (!isActive) {
header.addClass(settings.activeClass);
content.show();
}
});
});
};
})(cash); // or $ if aliased