shakeskeyboarde / slashes Goto Github PK
View Code? Open in Web Editor NEWAdd or strip backslashes.
License: Other
Add or strip backslashes.
License: Other
Hello,
First of all, thank you for this plugin.
I had trouble with the following snippet using \r\n
:
var slashes = require( "slashes" );
var str = "Hello,\\r\\nworld!";
console.log( slashes.strip( str ) );
The expected result was
Hello,
world!
But I got
Hello,r
world!
If set options.n = false, I would expect the sequence "\n" to be retained in the output. Instead, the sequence "\n" is interpreted as simply any other non-metacharacter, and replaced with "n". This behaviour should be switchable.
addSlashes('\ud83d\ude0a', { escapeNonAscii: true })
"😊" // should be '\\ud83d\\ude0a'
stripSlashes('\\ud83d\\ude0a')
"😊" // ok
problem: in str = str.replace(rx, char => {
,
the rx
regex does not match unicode chars, only chars from options.characters
my solution:
var backslashEncode = (function backslashEncodeFactory() {
const escapeCode = Array(32);
for (let i = 0; i < 32; i++)
escapeCode[i] = '\\x'+i.toString(16).padStart(2, '0');
escapeCode[0] = '\\0';
escapeCode[8] = '\\b'; escapeCode[9] = '\\t'; escapeCode[10] = '\\n';
escapeCode[11] = '\\v'; escapeCode[12] = '\\f'; escapeCode[13] = '\\r';
return function backslashEncode(str) {
let res = '';
for (let i = 0; i < str.length; i++) {
const char16bit = str[i];
const code = char16bit.charCodeAt(0);
res += (
(code < 32) ? escapeCode[code] : // ascii control
(code == 92) ? '\\\\' :
(code < 128) ? char16bit : // ascii printable
'\\u'+code.toString(16).padStart(4, '0') // unicode
);
}
return res;
}
})();
backslashEncode('\ud83d\ude0a')
'\\ud83d\\ude0a'
backslashEncode('\n \t \\ \r \b \v \f \0 ß ä € § ` ´')
'\\n \\t \\\\ \\r \\b \\v \\f \\0 \\u00df \\u00e4 \\u20ac \\u00a7 ` \\u00b4'
backslashEncode('\x19 \x18 \x17 \x16 \x15 \x14 \x13 \x12 \x11 \x10 \x0f \x0e')
'\\x19 \\x18 \\x17 \\x16 \\x15 \\x14 \\x13 \\x12 \\x11 \\x10 \\x0f \\x0e'
backslashEncode('\x0d \x0c \x0b \x0a \x09 \x08 \x07 \x06 \x05 \x04 \x03 \x02 \x01 \x00')
'\\r \\f \\v \\n \\t \\b \\x07 \\x06 \\x05 \\x04 \\x03 \\x02 \\x01 \\0'
i assume that array access is faster than map.get
could be made faster with a regex
that matches all 65536 char16bit
except the 95 printable ascii chars
like str = str.replace(/[^abcdefgh]/, char16bit => {
,
I noticed a new major version of this library was released recently but I'm not seeing any changelog or release notes to say what those changes are. Given that it's a major release presumably there are breaking changes?
The only license information is in package.json
file. It would be good to have a license file within the repository.
im trying to use slashes
in an ESM package
first node says
import { addSlashes, removeSlashes } from 'slashes'
^^^^^^^^^^
SyntaxError: Named export 'addSlashes' not found. The requested module 'slashes' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'slashes';
const { addSlashes, removeSlashes } = pkg;
fix: add "type": "module",
to node_modules/slashes/package.json
then node says
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'node_modules/slashes/lib/module/add-slashes' imported from node_modules/slashes/lib/module/index.js
fix: add .js
extension to imports
diff --git a/node_modules/slashes/lib/module/add-slashes.js b/node_modules/slashes/lib/module/add-slashes.js
index 14f33f5..a70f2f4 100644
--- a/node_modules/slashes/lib/module/add-slashes.js
+++ b/node_modules/slashes/lib/module/add-slashes.js
@@ -1,5 +1,5 @@
-import { getEscapedAny } from './get-escaped-any';
-import { getEscapedJsonUnsafe } from './get-escaped-json-unsafe';
+import { getEscapedAny } from './get-escaped-any.js';
+import { getEscapedJsonUnsafe } from './get-escaped-json-unsafe.js';
const addSlashes = (str, { getEscaped = getEscapedJsonUnsafe } = {}) => {
let result = '';
for (const char of str) {
diff --git a/node_modules/slashes/lib/module/index.js b/node_modules/slashes/lib/module/index.js
index 5c6d8bb..132076b 100644
--- a/node_modules/slashes/lib/module/index.js
+++ b/node_modules/slashes/lib/module/index.js
@@ -1,6 +1,6 @@
-export * from './add-slashes';
-export * from './get-escaped-any';
-export * from './get-escaped-json-unsafe';
-export * from './get-unescaped-any';
-export * from './remove-slashes';
+export * from './add-slashes.js';
+export * from './get-escaped-any.js';
+export * from './get-escaped-json-unsafe.js';
+export * from './get-unescaped-any.js';
+export * from './remove-slashes.js';
//# sourceMappingURL=index.js.map
diff --git a/node_modules/slashes/lib/module/remove-slashes.js b/node_modules/slashes/lib/module/remove-slashes.js
index 8b65849..3f60ef9 100644
--- a/node_modules/slashes/lib/module/remove-slashes.js
+++ b/node_modules/slashes/lib/module/remove-slashes.js
@@ -1,4 +1,4 @@
-import { getUnescapedAny } from './get-unescaped-any';
+import { getUnescapedAny } from './get-unescaped-any.js';
const removeSlashes = (source, { getUnescaped = getUnescapedAny } = {}) => {
const rx = /(?:(\\(u([0-9a-f]{4})|u\{([0-9a-f]+)\}|x([0-9a-f]{2})|(\d{1,3})|([\s\S]|$)))|([\s\S]))/giu;
let match;
to autofix the typescript imports see
https://github.com/milahu/random/blob/master/javascript/typescript-autofix-js-import-extension.js
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.