@tko/filter.punches
Version:
TKO filters from knockout punches
84 lines (81 loc) • 3.02 kB
JavaScript
// @tko/filter.punches 🥊 4.1.0 CommonJS
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var index_exports = {};
__export(index_exports, {
filters: () => filters
});
module.exports = __toCommonJS(index_exports);
// src/index.ts
var import_observable = require("@tko/observable");
var sproto = String.prototype;
var filters = {
// Convert value to uppercase
uppercase: function(value) {
return sproto.toUpperCase.call((0, import_observable.unwrap)(value));
},
// Convert value to lowercase
lowercase: function(value) {
return sproto.toLowerCase.call((0, import_observable.unwrap)(value));
},
// Return default value if the input value is empty or null
default: function(value, defaultValue) {
value = (0, import_observable.unwrap)(value);
if (typeof value === "function") {
return value;
}
if (typeof value === "string") {
return sproto.trim.call(value) === "" ? defaultValue : value;
}
return value == null || value.length == 0 ? defaultValue : value;
},
// Return the value with the search string replaced with the replacement string
replace: function(value, search, replace) {
return sproto.replace.call((0, import_observable.unwrap)(value), search, replace);
},
fit: function(value, length, replacement, trimWhere) {
value = (0, import_observable.unwrap)(value);
if (length && ("" + value).length > length) {
replacement = "" + (replacement || "...");
length = length - replacement.length;
value = "" + value;
switch (trimWhere) {
case "left":
return replacement + value.slice(-length);
case "middle": {
const leftLen = Math.ceil(length / 2);
return value.substring(0, leftLen) + replacement + value.slice(leftLen - length);
}
default:
return value.substring(0, length) + replacement;
}
} else {
return value;
}
},
// Convert a model object to JSON
json: function(rootObject, space, replacer) {
return JSON.stringify((0, import_observable.toJS)(rootObject), replacer, space);
},
// Format a number using the browser's toLocaleString
number: function(value) {
return (+(0, import_observable.unwrap)(value)).toLocaleString();
}
};