Utilities

Utilities

The small helpers that make everyday code shorter — all executed live below, so you can see exactly what they return.

Most of these are jQuery classics kept for compatibility ($.each, $.extend, $.param, $.trim, the $.is* checks). jsKjs adds a few modern essentials: $.escape() for safe HTML strings, $.uuid() backed by crypto.randomUUID(), and $.easings.

$.extend(true, …) deep-merges and refuses __proto__, constructor and prototype keys, so merging untrusted JSON can't pollute Object.prototype — the class of bug behind jQuery's CVE-2019-11358.

Live · Utilities console

Click the button — each line is real output.
View the source for this demo
HTML
<div class="row"><button type="button" id="run-utils">Run every utility</button></div>
<pre class="out" id="util-console" aria-live="polite">Click the button — each line is real output.</pre>
JavaScript
$('#run-utils').on('click', () => {
  const lines = [];
  const p = (call, value) => lines.push('<span class="k">' + $.escape(call) + '</span>\n  → ' + $.escape(String(value)));
  p('$.version', $.version);
  p("$.param({ a: 1, b: 'x y' })", $.param({ a: 1, b: 'x y' }));
  p("$.escape('<img onerror=alert(1)>')", $.escape('<img onerror=alert(1)>'));
  p('$.uuid()', $.uuid());
  p("$.trim('  padded  ')", "'" + $.trim('  padded  ') + "'");
  p('$.isArray([]) · $.isFunction($) · $.isPlainObject({})', [$.isArray([]), $.isFunction($), $.isPlainObject({})].join(' · '));
  p('$.map([1, 2], n => [n, n * 10])', JSON.stringify($.map([1, 2], n => [n, n * 10])));
  const seen = []; $.each({ a: 1, b: 2 }, (k, v) => seen.push(k + '=' + v));
  p('$.each({ a: 1, b: 2 }, fn)', seen.join(', '));
  p('$.extend(true, { a: { b: 1 } }, { a: { c: 2 } })', JSON.stringify($.extend(true, { a: { b: 1 } }, { a: { c: 2 } })));
  $.extend(true, {}, JSON.parse('{"__proto__": {"hacked": true}}'));
  p('prototype pollution attempt → ({}).hacked', ({}).hacked);
  p("$.parseHTML('<li>Hi</li>')[0].tagName", $.parseHTML('<li>Hi</li>')[0].tagName);
  p("Object.keys($.easings).length", Object.keys($.easings).length);
  $('#util-console').html(lines.join('\n'));
});

More examples

noConflict

JavaScript
// Another library owns $? Keep both.
const kj = $.noConflict();      // window.$ goes back to the old value
kj('#app').addClass('ready');   // jsKjs is also always available as window.kj and window.jsKjs

API

$.each(arrayOrObject, (keyOrIndex, value) => {})

Iterate arrays, array-likes, or plain objects.

Reference: $.each() →
$.map(collection, fn) → array

Map + flatten one level.

Reference: $.map() →
$.extend([deep], target, …sources) → target

Merge objects; pass true first for deep merge.

Reference: $.extend() →
$.param(object) → string

Serialize an object to a query string.

Reference: $.param() →
$.parseHTML(html) → element[]

Parse an HTML string into an array of nodes.

Reference: $.parseHTML() →
$.escape(string) → string

HTML-escape text — use it whenever you string-build HTML from user data.

Reference: $.escape() →
$.uuid() → string

A cryptographically strong UUID (v4).

Reference: $.uuid() →
$.isArray(x) · $.isFunction(x) · $.isPlainObject(x) · $.trim(s)

Small predicates and string trim, kept for jQuery compatibility.

Reference: Type checks →
$.noConflict() → $

Restore the previous window.$ and return jsKjs (also always available as kj and jsKjs).

Reference: $.noConflict() →
$.version → string

The library version.

Reference: $.version →

Migrating

Coming from jQueryjQuery 4 removed $.trim, $.isArray and $.isFunction; jsKjs keeps them so older code runs. $.Deferred, $.Callbacks, $.when and $.grep are not included — use Promises and Array.prototype.filter.

Related features