@thi.ng/strings
Advanced tools
Comparing version 0.7.1 to 1.0.0
14
case.js
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -8,3 +6,3 @@ * Uppercase string formatter. | ||
*/ | ||
exports.upper = (x) => x.toUpperCase(); | ||
export const upper = (x) => x.toUpperCase(); | ||
/** | ||
@@ -15,3 +13,3 @@ * Lowercase string formatter. | ||
*/ | ||
exports.lower = (x) => x.toLowerCase(); | ||
export const lower = (x) => x.toLowerCase(); | ||
/** | ||
@@ -22,3 +20,3 @@ * String formatter which capitalizes first character. | ||
*/ | ||
exports.capitalize = (x) => x[0].toUpperCase() + x.substr(1); | ||
export const capitalize = (x) => x[0].toUpperCase() + x.substr(1); | ||
/** | ||
@@ -41,3 +39,3 @@ * Converts a CamelCase string into kebab case, with optional custom | ||
*/ | ||
exports.kebab = (x, delim = "-") => exports.lower(x.replace(/(?<=[a-z0-9\u00e0-\u00fd])(?=[A-Z\u00c0-\u00dd])/g, (_, i) => (i ? delim : ""))); | ||
export const kebab = (x, delim = "-") => lower(x.replace(/(?<=[a-z0-9\u00e0-\u00fd])(?=[A-Z\u00c0-\u00dd])/g, (_, i) => (i ? delim : ""))); | ||
/** | ||
@@ -48,3 +46,3 @@ * Short for `kebab` using `_` as delimiter. | ||
*/ | ||
exports.snake = (x) => exports.kebab(x, "_"); | ||
export const snake = (x) => kebab(x, "_"); | ||
/** | ||
@@ -57,2 +55,2 @@ * Converts a kebab-case or snake_case string into CamelCase. Uses `-` | ||
*/ | ||
exports.camel = (x, delim = "-") => exports.lower(x).replace(new RegExp(`\\${delim}+(\\w)`, "g"), (_, c) => exports.upper(c)); | ||
export const camel = (x, delim = "-") => lower(x).replace(new RegExp(`\\${delim}+(\\w)`, "g"), (_, c) => upper(c)); |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
const repeat_1 = require("./repeat"); | ||
const truncate_1 = require("./truncate"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import { repeat } from "./repeat"; | ||
import { truncate } from "./truncate"; | ||
/** | ||
@@ -24,4 +22,4 @@ * Returns stringer which pads given input with `ch` (default: space) on | ||
*/ | ||
exports.center = memoizej_1.memoizeJ((n, pad = " ") => { | ||
const buf = repeat_1.repeat(pad, n); | ||
export const center = memoizeJ((n, pad = " ") => { | ||
const buf = repeat(pad, n); | ||
return (x) => { | ||
@@ -34,4 +32,4 @@ if (x == null) | ||
buf.substr(0, r) + x + buf.substr(0, r + ((n & 1) === (x.length & 1) ? 0 : 1)) : | ||
truncate_1.truncate(n)(x); | ||
truncate(n)(x); | ||
}; | ||
}); |
@@ -6,2 +6,29 @@ # Change Log | ||
# [1.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/strings@0.7.1...@thi.ng/strings@1.0.0) (2019-01-21) | ||
### Build System | ||
* update package build scripts & outputs, imports in ~50 packages ([b54b703](https://github.com/thi-ng/umbrella/commit/b54b703)) | ||
### Features | ||
* **strings:** add floatFixedWidth(), update float() ([816c9c0](https://github.com/thi-ng/umbrella/commit/816c9c0)) | ||
### BREAKING CHANGES | ||
* enabled multi-outputs (ES6 modules, CJS, UMD) | ||
- build scripts now first build ES6 modules in package root, then call | ||
`scripts/bundle-module` to build minified CJS & UMD bundles in `/lib` | ||
- all imports MUST be updated to only refer to package level | ||
(not individual files anymore). tree shaking in user land will get rid of | ||
all unused imported symbols. | ||
## [0.7.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/strings@0.7.0...@thi.ng/strings@0.7.1) (2018-12-15) | ||
@@ -8,0 +35,0 @@ |
import { Stringer } from "./api"; | ||
/** | ||
* Returns `Stringer` which formats numbers to given precision. | ||
* Exceptions: | ||
* | ||
* - NaN => "NaN" | ||
* - Infinity => "+/-∞" | ||
* | ||
* @param len number of fractional digits | ||
@@ -9,1 +13,8 @@ * @kind function | ||
export declare const float: (prec: number) => Stringer<number>; | ||
/** | ||
* Similar to `float`, returns `Stringer` which formats numbers to given | ||
* character width & precision. Uses scientific notation if needed. | ||
* | ||
* Default precision: 3 fractional digits | ||
*/ | ||
export declare const floatFixedWidth: (width: number, prec?: number) => Stringer<number>; |
43
float.js
@@ -1,10 +0,45 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import { padLeft } from "./pad-left"; | ||
/** | ||
* Returns `Stringer` which formats numbers to given precision. | ||
* Exceptions: | ||
* | ||
* - NaN => "NaN" | ||
* - Infinity => "+/-∞" | ||
* | ||
* @param len number of fractional digits | ||
* @kind function | ||
*/ | ||
exports.float = memoizej_1.memoizeJ((prec) => (x) => x.toFixed(prec)); | ||
export const float = memoizeJ((prec) => (x) => nanOrInf(x) || x.toFixed(prec)); | ||
/** | ||
* Similar to `float`, returns `Stringer` which formats numbers to given | ||
* character width & precision. Uses scientific notation if needed. | ||
* | ||
* Default precision: 3 fractional digits | ||
*/ | ||
export const floatFixedWidth = memoizeJ((width, prec = 3) => { | ||
const l = width - prec - 1; | ||
const pl = Math.pow(10, l); | ||
const pln = -Math.pow(10, l - 1); | ||
const pr = Math.pow(10, -(prec - 1)); | ||
const pad = padLeft(width); | ||
return (x) => { | ||
const ax = Math.abs(x); | ||
return pad(nanOrInf(x) || | ||
(x === 0 ? | ||
"0" : | ||
ax < pr || ax >= pl ? | ||
exp(x, width) : | ||
x.toFixed(prec - (x < pln ? 1 : 0)))); | ||
}; | ||
}); | ||
const exp = (x, w) => x.toExponential(Math.max(w - 4 - | ||
(Math.log(Math.abs(x)) / Math.LN10 >= 10 ? 2 : 1) | ||
- (x < 0 ? 1 : 0), 0)); | ||
const nanOrInf = (x) => isNaN(x) ? | ||
"NaN" : | ||
x === Infinity ? | ||
"+∞" : | ||
x === -Infinity ? | ||
"-∞" : | ||
undefined; |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.format = (fmt, ...args) => { | ||
export const format = (fmt, ...args) => { | ||
const acc = []; | ||
@@ -5,0 +3,0 @@ for (let i = 0, j = 0, n = fmt.length; i < n; i++) { |
37
index.js
@@ -1,21 +0,16 @@ | ||
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./case")); | ||
__export(require("./center")); | ||
__export(require("./float")); | ||
__export(require("./format")); | ||
__export(require("./pad-left")); | ||
__export(require("./pad-right")); | ||
__export(require("./parse")); | ||
__export(require("./percent")); | ||
__export(require("./radix")); | ||
__export(require("./repeat")); | ||
__export(require("./slugify")); | ||
__export(require("./splice")); | ||
__export(require("./truncate")); | ||
__export(require("./truncate-left")); | ||
__export(require("./units")); | ||
__export(require("./wrap")); | ||
export * from "./case"; | ||
export * from "./center"; | ||
export * from "./float"; | ||
export * from "./format"; | ||
export * from "./pad-left"; | ||
export * from "./pad-right"; | ||
export * from "./parse"; | ||
export * from "./percent"; | ||
export * from "./radix"; | ||
export * from "./repeat"; | ||
export * from "./slugify"; | ||
export * from "./splice"; | ||
export * from "./truncate"; | ||
export * from "./truncate-left"; | ||
export * from "./units"; | ||
export * from "./wrap"; |
{ | ||
"name": "@thi.ng/strings", | ||
"version": "0.7.1", | ||
"version": "1.0.0", | ||
"description": "Various string formatting & utility functions", | ||
"main": "./index.js", | ||
"module": "./index.js", | ||
"main": "./lib/index.js", | ||
"umd:main": "./lib/index.umd.js", | ||
"typings": "./index.d.ts", | ||
@@ -15,8 +17,10 @@ "repository": { | ||
"scripts": { | ||
"build": "yarn run clean && tsc --declaration", | ||
"clean": "rm -rf *.js *.d.ts .nyc_output build coverage doc", | ||
"build": "yarn clean && yarn build:es6 && yarn build:bundle", | ||
"build:es6": "tsc --declaration", | ||
"build:bundle": "../../scripts/bundle-module strings errors memoize", | ||
"test": "rimraf build && tsc -p test/tsconfig.json && nyc mocha build/test/*.js", | ||
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib", | ||
"cover": "yarn test && nyc report --reporter=lcov", | ||
"doc": "node_modules/.bin/typedoc --mode modules --out doc src", | ||
"pub": "yarn run build && yarn publish --access public", | ||
"test": "rm -rf build && tsc -p test && nyc mocha build/test/*.js" | ||
"pub": "yarn build && yarn publish --access public" | ||
}, | ||
@@ -28,18 +32,29 @@ "devDependencies": { | ||
"nyc": "^13.1.0", | ||
"typedoc": "^0.13.0", | ||
"typedoc": "^0.14.0", | ||
"typescript": "^3.2.2" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/errors": "^0.1.12", | ||
"@thi.ng/memoize": "^0.2.6" | ||
"@thi.ng/errors": "^1.0.0", | ||
"@thi.ng/memoize": "^1.0.0" | ||
}, | ||
"keywords": [ | ||
"composition", | ||
"ES6", | ||
"camelcase", | ||
"center", | ||
"float", | ||
"format", | ||
"functional", | ||
"hex", | ||
"higher order", | ||
"kebabcase", | ||
"number", | ||
"padding", | ||
"number", | ||
"percent", | ||
"radix", | ||
"slugify", | ||
"snakecase", | ||
"string", | ||
"typescript" | ||
"typescript", | ||
"wordwrap" | ||
], | ||
@@ -49,3 +64,4 @@ "publishConfig": { | ||
}, | ||
"gitHead": "159ce8f6b1d2dad1e12f2ba3f4f7b60d1623acee" | ||
"sideEffects": false, | ||
"gitHead": "348e7303b8b4d2749a02dd43e3f78d711242e4fe" | ||
} |
@@ -1,5 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
const repeat_1 = require("./repeat"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import { repeat } from "./repeat"; | ||
/** | ||
@@ -9,4 +7,4 @@ * @param n target length | ||
*/ | ||
exports.padLeft = memoizej_1.memoizeJ((n, ch = " ") => { | ||
const buf = repeat_1.repeat(ch, n); | ||
export const padLeft = memoizeJ((n, ch = " ") => { | ||
const buf = repeat(ch, n); | ||
return (x) => x != null ? | ||
@@ -13,0 +11,0 @@ (x = x.toString(), x.length < n ? buf.substr(x.length) + x : x) : |
@@ -1,5 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
const repeat_1 = require("./repeat"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import { repeat } from "./repeat"; | ||
/** | ||
@@ -9,4 +7,4 @@ * @param n target length | ||
*/ | ||
exports.padRight = memoizej_1.memoizeJ((n, ch = " ") => { | ||
const buf = repeat_1.repeat(ch, n); | ||
export const padRight = memoizeJ((n, ch = " ") => { | ||
const buf = repeat(ch, n); | ||
return (x) => x != null ? | ||
@@ -13,0 +11,0 @@ (x = x.toString(), x.length < n ? x + buf.substr(x.length) : x) : |
@@ -1,10 +0,8 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.maybeParseInt = (x, defaultVal = 0, radix = 10) => { | ||
export const maybeParseInt = (x, defaultVal = 0, radix = 10) => { | ||
const n = parseInt(x, radix); | ||
return isNaN(n) ? defaultVal : n; | ||
}; | ||
exports.maybeParseFloat = (x, defaultVal = 0) => { | ||
export const maybeParseFloat = (x, defaultVal = 0) => { | ||
const n = parseFloat(x); | ||
return isNaN(n) ? defaultVal : n; | ||
}; |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -9,2 +7,2 @@ * Returns `Stringer` which formats given fractions as percentage (e.g. | ||
*/ | ||
exports.percent = (prec = 0) => (x) => (x * 100).toFixed(prec) + "%"; | ||
export const percent = (prec = 0) => (x) => (x * 100).toFixed(prec) + "%"; |
22
radix.js
@@ -1,5 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
const repeat_1 = require("./repeat"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import { repeat } from "./repeat"; | ||
/** | ||
@@ -13,4 +11,4 @@ * Returns a `Stringer` which formats given numbers to `radix`, `len` | ||
*/ | ||
exports.radix = memoizej_1.memoizeJ((radix, n, prefix = "") => { | ||
const buf = repeat_1.repeat("0", n); | ||
export const radix = memoizeJ((radix, n, prefix = "") => { | ||
const buf = repeat("0", n); | ||
return (x) => { | ||
@@ -24,3 +22,3 @@ x = (x >>> 0).toString(radix); | ||
*/ | ||
exports.B8 = exports.radix(2, 8); | ||
export const B8 = radix(2, 8); | ||
/** | ||
@@ -30,3 +28,3 @@ * 8bit hex conversion preset. | ||
*/ | ||
exports.U8 = exports.radix(16, 2); | ||
export const U8 = radix(16, 2); | ||
/** | ||
@@ -36,3 +34,3 @@ * 16bit hex conversion preset. | ||
*/ | ||
exports.U16 = exports.radix(16, 4); | ||
export const U16 = radix(16, 4); | ||
/** | ||
@@ -42,3 +40,3 @@ * 24bit hex conversion preset. | ||
*/ | ||
exports.U24 = exports.radix(16, 6); | ||
export const U24 = radix(16, 6); | ||
/** | ||
@@ -48,3 +46,3 @@ * 32bit hex conversion preset. | ||
*/ | ||
exports.U32 = exports.radix(16, 8); | ||
export const U32 = radix(16, 8); | ||
/** | ||
@@ -54,2 +52,2 @@ * 64bit hex conversion preset (2x 32bit ints) | ||
*/ | ||
exports.U64 = (hi, lo) => exports.U32(hi) + exports.U32(lo); | ||
export const U64 = (hi, lo) => U32(hi) + U32(lo); |
@@ -23,4 +23,5 @@ # @thi.ng/strings | ||
Various higher-order, but low-level string formatting & utility | ||
functions, some memoized. WIP / Alpha. Please sources for now. | ||
Various higher-order, configurable string formatting & utility | ||
functions, some memoized. WIP / Alpha. Please sources / docstrings for | ||
now. | ||
@@ -27,0 +28,0 @@ ## Installation |
@@ -5,2 +5,2 @@ /** | ||
*/ | ||
export declare const repeat: import("@thi.ng/memoize/api").Fn2<string, number, string>; | ||
export declare const repeat: import("@thi.ng/memoize").Fn2<string, number, string>; |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
/** | ||
@@ -8,2 +6,2 @@ * @param ch character | ||
*/ | ||
exports.repeat = memoizej_1.memoizeJ((ch, n) => ch.repeat(n)); | ||
export const repeat = memoizeJ((ch, n) => ch.repeat(n)); |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const src = "àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;"; | ||
@@ -12,3 +10,3 @@ const dest = "aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------"; | ||
*/ | ||
exports.slugify = (str) => { | ||
export const slugify = (str) => { | ||
return str | ||
@@ -15,0 +13,0 @@ .toLowerCase() |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const illegal_arguments_1 = require("@thi.ng/errors/illegal-arguments"); | ||
import { illegalArgs } from "@thi.ng/errors"; | ||
/** | ||
@@ -17,3 +15,3 @@ * Forms a new strings which inserts given `insert` string into `src` | ||
*/ | ||
exports.splice = (src, insert, from, to = from) => { | ||
export const splice = (src, insert, from, to = from) => { | ||
if (from < 0) { | ||
@@ -26,3 +24,3 @@ from += src.length; | ||
if (from > to) { | ||
illegal_arguments_1.illegalArgs("'from' index must be <= 'to'"); | ||
illegalArgs("'from' index must be <= 'to'"); | ||
} | ||
@@ -29,0 +27,0 @@ to = Math.max(to, 0); |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
exports.truncateLeft = memoizej_1.memoizeJ((n, prefix = "") => (x) => x.length > n ? | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
export const truncateLeft = memoizeJ((n, prefix = "") => (x) => x.length > n ? | ||
prefix + x.substr(x.length - n + prefix.length) : | ||
x); |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
exports.truncate = memoizej_1.memoizeJ((n, suffix = "") => (x) => x.length > n ? | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
export const truncate = memoizeJ((n, suffix = "") => (x) => x.length > n ? | ||
x.substr(0, n - suffix.length) + suffix : | ||
x); |
16
units.js
@@ -1,5 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
exports.units = memoizej_1.memoizeJ((exp, base, prec = 2) => { | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
export const units = memoizeJ((exp, base, prec = 2) => { | ||
const groups = exp.map((x) => [x[0], x[2] != null ? x[2] : prec, x[1]]) | ||
@@ -22,3 +20,3 @@ .sort((a, b) => a[0] - b[0]); | ||
const MB = 1024 * 1024; | ||
exports.bits = exports.units([ | ||
export const bits = units([ | ||
[1, " bits", 0], | ||
@@ -29,3 +27,3 @@ [KB, " Kb"], | ||
], " bits", 2); | ||
exports.bytes = exports.units([ | ||
export const bytes = units([ | ||
[1, " bytes", 0], | ||
@@ -38,3 +36,3 @@ [KB, " KB"], | ||
], " bytes", 2); | ||
exports.seconds = exports.units([ | ||
export const seconds = units([ | ||
[1e-12, " ps"], | ||
@@ -49,3 +47,3 @@ [1e-9, " ns"], | ||
], " secs", 3); | ||
exports.meters = exports.units([ | ||
export const meters = units([ | ||
[1e-12, " pm"], | ||
@@ -59,3 +57,3 @@ [1e-9, " nm"], | ||
], " m", 2); | ||
exports.grams = exports.units([ | ||
export const grams = units([ | ||
[1e-12, " pg"], | ||
@@ -62,0 +60,0 @@ [1e-9, " ng"], |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const memoizej_1 = require("@thi.ng/memoize/memoizej"); | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
/** | ||
@@ -8,2 +6,2 @@ * Returns a `Stringer` which wrap inputs with given `pad` string on | ||
*/ | ||
exports.wrap = memoizej_1.memoizeJ((pad) => (x) => pad + x + pad); | ||
export const wrap = memoizeJ((pad) => (x) => pad + x + pad); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
71662
44
1055
1
50
+ Added@thi.ng/api@6.13.6(transitive)
+ Added@thi.ng/errors@1.3.4(transitive)
+ Added@thi.ng/memoize@1.1.8(transitive)
- Removed@thi.ng/api@4.2.4(transitive)
- Removed@thi.ng/errors@0.1.12(transitive)
- Removed@thi.ng/memoize@0.2.6(transitive)
Updated@thi.ng/errors@^1.0.0
Updated@thi.ng/memoize@^1.0.0