string-normalize-space
Advanced tools
+41
-25
@@ -1,40 +0,56 @@ | ||
| module.exports = { | ||
| const normalizeSpace = s => { | ||
| normalizeSpace: s => { | ||
| if (typeof s !== 'string') throw Error ('Expected a string, got: ' + s) | ||
| s = s.trim () | ||
| if (typeof s !== 'string') throw Error ('Expected a string, got: ' + s) | ||
| s = s.trim () | ||
| const {length} = s | ||
| const {length} = s | ||
| let result = '', from = 0, to = 0, sp = false; while (to < length) { | ||
| let result = '', from = 0, to = 0, sp = false; while (to < length) { | ||
| const c = s.charCodeAt (to) | ||
| const c = s.charCodeAt (to) | ||
| switch (c) { | ||
| switch (c) { | ||
| case 0x20: | ||
| case 0x9: | ||
| case 0xD: | ||
| case 0xA: | ||
| if (!sp) result += s.slice (from, to) + ' ' | ||
| from = ++ to | ||
| sp = true | ||
| break | ||
| case 0x20: | ||
| case 0x9: | ||
| case 0xD: | ||
| case 0xA: | ||
| if (!sp) result += s.slice (from, to) + ' ' | ||
| from = ++ to | ||
| sp = true | ||
| break | ||
| default: | ||
| if (sp) sp = false | ||
| to ++ | ||
| default: | ||
| if (sp) sp = false | ||
| to ++ | ||
| } | ||
| } | ||
| if (from === 0) return s | ||
| } | ||
| return (result + s.slice (from)).trim () | ||
| if (from === 0) return s | ||
| return (result + s.slice (from)).trim () | ||
| } | ||
| class NormalizeSpaceLogFormat { | ||
| transform (info) { | ||
| info.message = normalizeSpace (info.message) | ||
| return info | ||
| } | ||
| } | ||
| module.exports = { | ||
| normalizeSpace, | ||
| logform: () => new NormalizeSpaceLogFormat () | ||
| } |
+6
-4
| { | ||
| "name": "string-normalize-space", | ||
| "version": "1.0.0", | ||
| "description": "XPath 1.0 normalize-space analog", | ||
| "version": "1.0.1", | ||
| "description": "XPath 1.0 normalize-space analog, winston log formatter", | ||
| "main": "index.js", | ||
@@ -36,3 +36,4 @@ "files": [ | ||
| "normalize", | ||
| "space" | ||
| "space", | ||
| "winston" | ||
| ], | ||
@@ -45,5 +46,6 @@ "author": "Dmitry Ovsyanko", | ||
| "devDependencies": { | ||
| "jest": "^29.3.1" | ||
| "jest": "^29.3.1", | ||
| "winston": "^3.14.0" | ||
| }, | ||
| "homepage": "https://github.com/do-/node-string-normalize-space#readme" | ||
| } |
+22
-1
@@ -6,4 +6,6 @@  | ||
| So it's basically like [String.prototype.trim()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim), but works also inside the string. | ||
| So it's basically like [String.prototype.trim()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim), but also works inside the string. | ||
| The [`winston`](https://github.com/winstonjs/winston) compatible log formatter is provided. | ||
| # Installation | ||
@@ -16,2 +18,3 @@ | ||
| # Usage | ||
| ## General purpose | ||
@@ -24,2 +27,20 @@ ```js | ||
| ## As a `winston` log formatter | ||
| ```js | ||
| const {createLogger, format: {combine, printf}, transports} = require ('winston') | ||
| const normalizeSpaceLogFormat = require ('string-normalize-space').logform | ||
| const logger = createLogger ({ | ||
| transports: [ | ||
| new transports.Console (), | ||
| ], | ||
| format: combine ( | ||
| printf (info => `${info.message} ${info.meta} ${info.stack}`), // may be lots of \n | ||
| normalizeSpaceLogFormat () // exactly one line | ||
| ), | ||
| }) | ||
| ``` | ||
| # Notes | ||
@@ -26,0 +47,0 @@ |
27718
3.18%32
33.33%48
77.78%2
100%