Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
2af0329918 | |||
b65a460ef2 | |||
3f5b0ebe1e | |||
6f2cedc1dd | |||
2c29ae3bf0 | |||
b26b8ed4a6 | |||
0e6877d2d1 | |||
8a0c3fe906 | |||
ae861fa271 | |||
3e78466e5f | |||
eea19bcd89 | |||
1fc95fcf0e |
@ -119,6 +119,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
npmci node install stable
|
npmci node install stable
|
||||||
npmci npm install
|
npmci npm install
|
||||||
pnpm install -g @gitzone/tsdoc
|
pnpm install -g @git.zone/tsdoc
|
||||||
npmci command tsdoc
|
npmci command tsdoc
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
|
28
package.json
28
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartstring",
|
"name": "@push.rocks/smartstring",
|
||||||
"version": "4.0.8",
|
"version": "4.0.15",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "handle strings in smart ways. TypeScript ready.",
|
"description": "handle strings in smart ways. TypeScript ready.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -13,7 +13,7 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://gitlab.com/pushrocks/smartstring.git"
|
"url": "git+https://gitlab.com/push.rocks/smartstring.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"regex",
|
"regex",
|
||||||
@ -22,27 +22,25 @@
|
|||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitlab.com/pushrocks/smartstring/issues"
|
"url": "https://gitlab.com/push.rocks/smartstring/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartstring#readme",
|
"homepage": "https://gitlab.com/push.rocks/smartstring#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.66",
|
"@git.zone/tsbuild": "^2.1.72",
|
||||||
"@gitzone/tsrun": "^1.2.42",
|
"@git.zone/tsrun": "^1.2.42",
|
||||||
"@gitzone/tstest": "^1.0.74",
|
"@git.zone/tstest": "^1.0.86",
|
||||||
"@push.rocks/tapbundle": "^5.0.8",
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
"@types/node": "^20.3.1"
|
"@types/node": "^20.11.24"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/isounique": "^1.0.5",
|
"@push.rocks/isounique": "^1.0.5",
|
||||||
"@push.rocks/smartenv": "^5.0.5",
|
"@push.rocks/smartenv": "^5.0.12",
|
||||||
"@types/randomatic": "^3.1.3",
|
"@types/randomatic": "^3.1.5",
|
||||||
"buffer": "^6.0.3",
|
|
||||||
"crypto-random-string": "^5.0.0",
|
"crypto-random-string": "^5.0.0",
|
||||||
"js-base64": "^3.7.5",
|
"js-base64": "^3.7.7",
|
||||||
"normalize-newline": "^4.1.0",
|
|
||||||
"randomatic": "^3.1.1",
|
"randomatic": "^3.1.1",
|
||||||
"strip-indent": "^4.0.0",
|
"strip-indent": "^4.0.0",
|
||||||
"url": "^0.11.1"
|
"url": "^0.11.3"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
3179
pnpm-lock.yaml
generated
3179
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -2,15 +2,16 @@ import { tap, expect } from '@push.rocks/tapbundle';
|
|||||||
|
|
||||||
import * as smartstring from '../ts/index.js';
|
import * as smartstring from '../ts/index.js';
|
||||||
|
|
||||||
tap.test('should normalize a string', async () => {
|
tap.test('should normalize a string', async (toolsArg) => {
|
||||||
const testString = `
|
const testString = `
|
||||||
myawesome string;
|
myawesome string;
|
||||||
is indented with two spaces
|
is indented with two spaces
|
||||||
`;
|
`;
|
||||||
const normalizedString = smartstring.normalize.standard(testString);
|
const normalizedString = smartstring.normalize.standard(testString);
|
||||||
|
console.log(normalizedString);
|
||||||
|
|
||||||
expect(normalizedString).toEqual(
|
expect(normalizedString).toEqual(
|
||||||
`
|
`myawesome string;
|
||||||
myawesome string;
|
|
||||||
is indented with two spaces
|
is indented with two spaces
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstring',
|
name: '@push.rocks/smartstring',
|
||||||
version: '4.0.8',
|
version: '4.0.15',
|
||||||
description: 'handle strings in smart ways. TypeScript ready.'
|
description: 'handle strings in smart ways. TypeScript ready.'
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,42 @@ export const replaceAll = (stringArg: string, searchPattern: string, replacement
|
|||||||
return stringArg.replace(new RegExp(searchPattern, 'g'), replacementString);
|
return stringArg.replace(new RegExp(searchPattern, 'g'), replacementString);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface INormalizeOptions {
|
||||||
|
stripLeadingTrailingEmptyLines?: boolean;
|
||||||
|
stripAllEmptyLines?: boolean;
|
||||||
|
stripIndent?: boolean;
|
||||||
|
normalizeNewline?: boolean;
|
||||||
|
replaceTabs?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* normalizes a string
|
* Normalizes a string
|
||||||
* @param stringArg
|
* @param stringArg
|
||||||
|
* @param options
|
||||||
*/
|
*/
|
||||||
export const standard = (stringArg: string): string => {
|
export const standard = (stringArg: string, options?: INormalizeOptions): string => {
|
||||||
let fix1 = plugins.stripIndent(stringArg); // fix indention
|
let result = stringArg;
|
||||||
let fix2 = plugins.normalizeNewline(fix1); // fix newlines
|
|
||||||
let fix3 = replaceAll(fix2, '\t/', ' '); // fix tabs
|
if (!options || options.stripIndent) {
|
||||||
return fix3;
|
result = plugins.stripIndent(result); // fix indention
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options || options.normalizeNewline) {
|
||||||
|
result = result.replace(/\r\n/g, '\n'); // fix newlines
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options || options.replaceTabs) {
|
||||||
|
result = replaceAll(result, '\t/', ' '); // fix tabs
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options || options.stripLeadingTrailingEmptyLines) {
|
||||||
|
result = result.replace(/^\s*[\r\n]/gm, '').replace(/\s*[\r\n]$/gm, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options || options.stripAllEmptyLines) {
|
||||||
|
result = result.replace(/^\s*[\r\n]/gm, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,10 +5,6 @@ import * as isounique from '@push.rocks/isounique';
|
|||||||
|
|
||||||
export { isounique };
|
export { isounique };
|
||||||
|
|
||||||
import { Buffer } from 'buffer';
|
|
||||||
if (smartenvInstance.isBrowser) {
|
|
||||||
globalThis.Buffer = Buffer;
|
|
||||||
}
|
|
||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
export { url };
|
export { url };
|
||||||
|
|
||||||
@ -16,7 +12,6 @@ export { url };
|
|||||||
import { Base64 as jsBase64 } from 'js-base64';
|
import { Base64 as jsBase64 } from 'js-base64';
|
||||||
|
|
||||||
import stripIndent from 'strip-indent';
|
import stripIndent from 'strip-indent';
|
||||||
import normalizeNewline from 'normalize-newline';
|
|
||||||
import randomatic from 'randomatic';
|
import randomatic from 'randomatic';
|
||||||
|
|
||||||
export { jsBase64, stripIndent, normalizeNewline, randomatic };
|
export { jsBase64, stripIndent, randomatic };
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import * as plugins from './smartstring.plugins.js';
|
import * as plugins from './smartstring.plugins.js';
|
||||||
import * as base64 from './smartstring.base64.js';
|
import * as base64 from './smartstring.base64.js';
|
||||||
|
|
||||||
export const isUtf8 = (stringArg: string) => {
|
export const isUtf8 = (stringArg: string): boolean => {
|
||||||
const bytes = Buffer.from(stringArg);
|
// Convert string to a Uint8Array. In browsers, this will be a bit more complex
|
||||||
|
// because we need to convert the string to a binary representation first.
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const bytes = encoder.encode(stringArg);
|
||||||
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
while (i < bytes.length) {
|
while (i < bytes.length) {
|
||||||
if (
|
if (
|
||||||
@ -33,12 +37,14 @@ export const isUtf8 = (stringArg: string) => {
|
|||||||
0xa0 <= bytes[i + 1] &&
|
0xa0 <= bytes[i + 1] &&
|
||||||
bytes[i + 1] <= 0xbf &&
|
bytes[i + 1] <= 0xbf &&
|
||||||
0x80 <= bytes[i + 2] &&
|
0x80 <= bytes[i + 2] &&
|
||||||
bytes[i + 2] <= 0xbf) || // straight 3-byte
|
bytes[i + 2] <= 0xbf) ||
|
||||||
|
// straight 3-byte
|
||||||
(((0xe1 <= bytes[i] && bytes[i] <= 0xec) || bytes[i] === 0xee || bytes[i] === 0xef) &&
|
(((0xe1 <= bytes[i] && bytes[i] <= 0xec) || bytes[i] === 0xee || bytes[i] === 0xef) &&
|
||||||
0x80 <= bytes[i + 1] &&
|
0x80 <= bytes[i + 1] &&
|
||||||
bytes[i + 1] <= 0xbf &&
|
bytes[i + 1] <= 0xbf &&
|
||||||
0x80 <= bytes[i + 2] &&
|
0x80 <= bytes[i + 2] &&
|
||||||
bytes[i + 2] <= 0xbf) || // excluding surrogates
|
bytes[i + 2] <= 0xbf) ||
|
||||||
|
// excluding surrogates
|
||||||
(bytes[i] === 0xed &&
|
(bytes[i] === 0xed &&
|
||||||
0x80 <= bytes[i + 1] &&
|
0x80 <= bytes[i + 1] &&
|
||||||
bytes[i + 1] <= 0x9f &&
|
bytes[i + 1] <= 0x9f &&
|
||||||
@ -57,7 +63,8 @@ export const isUtf8 = (stringArg: string) => {
|
|||||||
0x80 <= bytes[i + 2] &&
|
0x80 <= bytes[i + 2] &&
|
||||||
bytes[i + 2] <= 0xbf &&
|
bytes[i + 2] <= 0xbf &&
|
||||||
0x80 <= bytes[i + 3] &&
|
0x80 <= bytes[i + 3] &&
|
||||||
bytes[i + 3] <= 0xbf) || // planes 4-15
|
bytes[i + 3] <= 0xbf) ||
|
||||||
|
// planes 4-15
|
||||||
(0xf1 <= bytes[i] &&
|
(0xf1 <= bytes[i] &&
|
||||||
bytes[i] <= 0xf3 &&
|
bytes[i] <= 0xf3 &&
|
||||||
0x80 <= bytes[i + 1] &&
|
0x80 <= bytes[i + 1] &&
|
||||||
@ -65,7 +72,8 @@ export const isUtf8 = (stringArg: string) => {
|
|||||||
0x80 <= bytes[i + 2] &&
|
0x80 <= bytes[i + 2] &&
|
||||||
bytes[i + 2] <= 0xbf &&
|
bytes[i + 2] <= 0xbf &&
|
||||||
0x80 <= bytes[i + 3] &&
|
0x80 <= bytes[i + 3] &&
|
||||||
bytes[i + 3] <= 0xbf) || // plane 16
|
bytes[i + 3] <= 0xbf) ||
|
||||||
|
// plane 16
|
||||||
(bytes[i] === 0xf4 &&
|
(bytes[i] === 0xf4 &&
|
||||||
0x80 <= bytes[i + 1] &&
|
0x80 <= bytes[i + 1] &&
|
||||||
bytes[i + 1] <= 0x8f &&
|
bytes[i + 1] <= 0x8f &&
|
||||||
@ -84,6 +92,7 @@ export const isUtf8 = (stringArg: string) => {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const isBase64 = (stringArg: string) => {
|
export const isBase64 = (stringArg: string) => {
|
||||||
const notBase64 = /[^A-Z0-9+\/=]/i;
|
const notBase64 = /[^A-Z0-9+\/=]/i;
|
||||||
const len = stringArg.length;
|
const len = stringArg.length;
|
||||||
|
@ -3,9 +3,12 @@
|
|||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"useDefineForClassFields": false,
|
"useDefineForClassFields": false,
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "ES2022",
|
"module": "NodeNext",
|
||||||
"moduleResolution": "nodenext",
|
"moduleResolution": "NodeNext",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"verbatimModuleSyntax": true,
|
"verbatimModuleSyntax": true
|
||||||
}
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user