@zodash/format
Advanced tools
Comparing version 1.2.12 to 1.3.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.3.0](https://github.com/zcorky/zodash/compare/@zodash/format@1.2.12...@zodash/format@1.3.0) (2022-05-28) | ||
### Features | ||
* **format:** support default value and builtin url template (/users/:id/xxx) ([3982d99](https://github.com/zcorky/zodash/commit/3982d99ba86ec9bf20ac341301186954708db34a)) | ||
## [1.2.12](https://github.com/zcorky/zodash/compare/@zodash/format@1.2.11...@zodash/format@1.2.12) (2022-01-13) | ||
@@ -8,0 +19,0 @@ |
@@ -5,2 +5,6 @@ export interface Seperator { | ||
} | ||
export interface FormatOptions { | ||
seperator?: Seperator; | ||
default?: string; | ||
} | ||
/** | ||
@@ -17,2 +21,3 @@ * Format string | ||
*/ | ||
export declare function format(text: string, mapOrFn: Record<string, any> | ((key: string, text: string) => any), seperator?: Seperator): string; | ||
export declare function format(text: string, mapOrFn: Record<string, any> | ((key: string, text: string) => any), options?: FormatOptions): string; | ||
export declare function formatUrl(urlTemplate: string, mapOrFn: Record<string, any> | ((key: string, text: string) => any)): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.format = void 0; | ||
exports.formatUrl = exports.format = void 0; | ||
const get_1 = require("@zodash/get"); | ||
@@ -20,3 +20,10 @@ const DEFAULT_SEPERATOR = { | ||
*/ | ||
function format(text, mapOrFn, seperator = DEFAULT_SEPERATOR) { | ||
function format(text, mapOrFn, options) { | ||
var _a; | ||
const seperator = (_a = options === null || options === void 0 ? void 0 : options.seperator) !== null && _a !== void 0 ? _a : DEFAULT_SEPERATOR; | ||
const defaultValue = (options === null || options === void 0 ? void 0 : options.default) || ''; | ||
// /users/:id/profile | ||
if (text.indexOf('/:') != -1) { | ||
return formatUrl(text, mapOrFn); | ||
} | ||
const seperatorStart = seperator.start; | ||
@@ -28,4 +35,11 @@ const SeperatorEnd = seperator.end; | ||
} | ||
return text.replace(pattern, (_, key) => get_1.get(mapOrFn, key, '')); | ||
return text.replace(pattern, (_, key) => get_1.get(mapOrFn, key, defaultValue)); | ||
} | ||
exports.format = format; | ||
function formatUrl(urlTemplate, mapOrFn) { | ||
if (typeof mapOrFn === 'function') { | ||
return urlTemplate.replace(/:([^/]+)/g, (_, key) => mapOrFn(key, urlTemplate)); | ||
} | ||
return urlTemplate.replace(/:([^/]+)/g, (_, key) => get_1.get(mapOrFn, key)); | ||
} | ||
exports.formatUrl = formatUrl; |
{ | ||
"name": "@zodash/format", | ||
"version": "1.2.12", | ||
"version": "1.3.0", | ||
"description": "format string with object values", | ||
@@ -69,3 +69,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "8506a4be416c68bf066e94234fb1367010a0d716", | ||
"gitHead": "2925ad7fa89bbc2de85c0c0a58964a6cbaa1507c", | ||
"devDependencies": { | ||
@@ -72,0 +72,0 @@ "@zodash/get": "^1.1.3" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11017
64