string-byte-length
Advanced tools
Comparing version
export const getNodeByteLength=function(string){ | ||
export const getNodeByteLength=(string)=> | ||
return globalThis.Buffer.byteLength(string); | ||
}; | ||
//# sourceMappingURL=buffer.js.map | ||
globalThis.Buffer.byteLength(string); |
@@ -5,3 +5,3 @@ | ||
export const getCharCodeByteLength=function(string){ | ||
export const getCharCodeByteLength=(string)=>{ | ||
const charLength=string.length; | ||
@@ -14,3 +14,3 @@ let byteLength=charLength; | ||
if(codepoint<=LAST_ASCII_CODEPOINT){ | ||
continue; | ||
continue | ||
} | ||
@@ -20,3 +20,3 @@ | ||
byteLength+=1; | ||
continue; | ||
continue | ||
} | ||
@@ -27,3 +27,3 @@ | ||
if(codepoint<FIRST_HIGH_SURROGATE||codepoint>LAST_HIGH_SURROGATE){ | ||
continue; | ||
continue | ||
} | ||
@@ -43,9 +43,9 @@ | ||
{ | ||
continue; | ||
continue | ||
} | ||
charIndex+=1; | ||
charIndex+=1 | ||
} | ||
return byteLength; | ||
return byteLength | ||
}; | ||
@@ -64,3 +64,2 @@ | ||
const FIRST_LOW_SURROGATE=56320; | ||
const LAST_LOW_SURROGATE=57343; | ||
//# sourceMappingURL=char_code.js.map | ||
const LAST_LOW_SURROGATE=57343; |
export const createTextEncoderFunc=function(){ | ||
return getTextEncoderByteLength.bind(undefined,new TextEncoder); | ||
}; | ||
export const createTextEncoderFunc=()=> | ||
getTextEncoderByteLength.bind(undefined,new TextEncoder); | ||
const getTextEncoderByteLength=function(textEncoder,string){ | ||
const getTextEncoderByteLength=(textEncoder,string)=>{ | ||
const encoderBuffer=getBuffer(string); | ||
return textEncoder.encodeInto(string,encoderBuffer).written; | ||
return textEncoder.encodeInto(string,encoderBuffer).written | ||
}; | ||
const getBuffer=function(string){ | ||
const getBuffer=(string)=>{ | ||
const size=string.length*3; | ||
if(size>CACHE_MAX_MEMORY){ | ||
return new Uint8Array(size); | ||
return new Uint8Array(size) | ||
} | ||
@@ -22,6 +21,6 @@ | ||
cachedEncoderBuffer=new Uint8Array(size); | ||
cachedEncoderBuffer=new Uint8Array(size) | ||
} | ||
return cachedEncoderBuffer; | ||
return cachedEncoderBuffer | ||
}; | ||
@@ -35,3 +34,2 @@ | ||
export const TEXT_ENCODER_MIN_LENGTH=100; | ||
//# sourceMappingURL=encoder.js.map | ||
export const TEXT_ENCODER_MIN_LENGTH=100; |
@@ -6,22 +6,20 @@ import{getNodeByteLength}from"./buffer.js"; | ||
const getMainFunction=function(){ | ||
const getMainFunction=()=>{ | ||
if("Buffer"in globalThis&&"byteLength"in globalThis.Buffer){ | ||
return getNodeByteLength; | ||
return getNodeByteLength | ||
} | ||
if("TextEncoder"in globalThis){ | ||
return getByteLength.bind(undefined,createTextEncoderFunc()); | ||
return getByteLength.bind(undefined,createTextEncoderFunc()) | ||
} | ||
return getCharCodeByteLength; | ||
return getCharCodeByteLength | ||
}; | ||
const getByteLength=function(getTextEncoderByteLength,string){ | ||
return string.length<TEXT_ENCODER_MIN_LENGTH? | ||
const getByteLength=(getTextEncoderByteLength,string)=> | ||
string.length<TEXT_ENCODER_MIN_LENGTH? | ||
getCharCodeByteLength(string): | ||
getTextEncoderByteLength(string); | ||
}; | ||
export default getMainFunction(); | ||
//# sourceMappingURL=main.js.map | ||
export default getMainFunction(); |
{ | ||
"name": "string-byte-length", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"type": "module", | ||
"exports": { | ||
"types": "./build/types/main.d.ts", | ||
"types": "./build/src/main.d.ts", | ||
"default": "./build/src/main.js" | ||
}, | ||
"main": "./build/src/main.js", | ||
"types": "./build/types/main.d.ts", | ||
"types": "./build/src/main.d.ts", | ||
"files": [ | ||
"build/src/**/*.{js,json}", | ||
"build/types/**/*.d.ts" | ||
"build/src/**/*.{js,json,d.ts}", | ||
"!build/src/**/*.test.js", | ||
"!build/src/{helpers,fixtures}" | ||
], | ||
@@ -50,13 +51,12 @@ "sideEffects": false, | ||
"directories": { | ||
"lib": "src", | ||
"test": "test" | ||
"lib": "src" | ||
}, | ||
"devDependencies": { | ||
"@ehmicky/dev-tasks": "^1.0.102", | ||
"spyd": "^0.6.0", | ||
"test-each": "^5.5.0" | ||
"@ehmicky/dev-tasks": "^2.0.80", | ||
"spyd": "^0.6.1", | ||
"test-each": "^6.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=14.18.0" | ||
"node": ">=16.17.0" | ||
} | ||
} |
[](https://www.npmjs.com/package/string-byte-length) | ||
[](https://unpkg.com/string-byte-length?module) | ||
[](/types/main.d.ts) | ||
[](/src/main.d.ts) | ||
[](https://codecov.io/gh/ehmicky/string-byte-length) | ||
[](https://bundlephobia.com/package/string-byte-length) | ||
[](https://twitter.com/intent/follow?screen_name=ehmicky) | ||
[](https://fosstodon.org/@ehmicky) | ||
[](https://medium.com/@ehmicky) | ||
@@ -35,7 +35,10 @@ | ||
This package works in both Node.js >=14.18.0 and | ||
[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/tasks/build/browserslist). | ||
It is an ES module and must be loaded using | ||
This package works in both Node.js >=16.17.0 and | ||
[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist). | ||
This is an ES module. It must be loaded using | ||
[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c), | ||
not `require()`. | ||
not `require()`. If TypeScript is used, it must be configured to | ||
[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html), | ||
not CommonJS. | ||
@@ -185,4 +188,4 @@ # Alternatives | ||
<!-- | ||
<table><tr><td align="center"><a href="https://twitter.com/ehmicky"><img src="https://avatars2.githubusercontent.com/u/8136211?v=4" width="100px;" alt="ehmicky"/><br /><sub><b>ehmicky</b></sub></a><br /><a href="https://github.com/ehmicky/string-byte-length/commits?author=ehmicky" title="Code">💻</a> <a href="#design-ehmicky" title="Design">🎨</a> <a href="#ideas-ehmicky" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ehmicky/string-byte-length/commits?author=ehmicky" title="Documentation">📖</a></td></tr></table> | ||
<table><tr><td align="center"><a href="https://fosstodon.org/@ehmicky"><img src="https://avatars2.githubusercontent.com/u/8136211?v=4" width="100px;" alt="ehmicky"/><br /><sub><b>ehmicky</b></sub></a><br /><a href="https://github.com/ehmicky/string-byte-length/commits?author=ehmicky" title="Code">💻</a> <a href="#design-ehmicky" title="Design">🎨</a> <a href="#ideas-ehmicky" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ehmicky/string-byte-length/commits?author=ehmicky" title="Documentation">📖</a></td></tr></table> | ||
--> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> |
190
1.6%15928
-0.5%83
-7.78%