@texting/charset-fullwidth
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -5,4 +5,3 @@ 'use strict'; | ||
var charsetAnsi = require('@texting/charset-ansi'); | ||
var enumFullAngleChars = require('@texting/enum-full-angle-chars'); | ||
var enumChars = require('@texting/enum-chars'); | ||
@@ -27,12 +26,32 @@ const CJK_PUNCS = '\u3000-\u303f'; | ||
const DELTA_FULL = 0xfee0; // export const REG_NUM_FULL = /^\s*[-+]?(?:,*[0-9]+)*.?[0-9]+\s*$/ | ||
const REG_FULL = new RegExp(`[${CJK_PUNCS}${FULL_CHARS}]+`, 'g'); // /[\uff01-\uff5e|\u3000]+/g | ||
const REG_NUM_FULL = new RegExp(`^\s*[-+]?(?:,*[${FULL_NUM}]+)*.?[${FULL_NUM}]+\s*$`); | ||
const LEAN_REG = /(\W)\s+/g; | ||
/** | ||
* Half-angle string -> Full-angle string | ||
* 半角转化为全角 | ||
* a.全角空格为12288,半角空格为32 | ||
* b.其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 | ||
* | ||
* @param {string} tx | ||
* @returns {boolean} | ||
*/ | ||
const isNumeric = tx => REG_NUM_FULL.test(tx); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Full-angle string -> Half-angle string | ||
* 全角转换为半角 | ||
* @param {string} text | ||
@@ -43,50 +62,96 @@ * @returns {string} | ||
const _halfToFullCore = text => { | ||
let l = text === null || text === void 0 ? void 0 : text.length, | ||
function _fullToHalfCore(text) { | ||
const { | ||
conv | ||
} = this; | ||
let ms, | ||
l = 0, | ||
r = 0, | ||
sp, | ||
ph, | ||
body = ''; | ||
while ((ms = REG_FULL.exec(text)) && ([ph] = ms)) { | ||
r = ms.index; | ||
if (l !== r && (sp = text.slice(l, r))) body += sp; | ||
body += conv(ph); | ||
l = REG_FULL.lastIndex; | ||
} | ||
if (l < (text === null || text === void 0 ? void 0 : text.length)) body += text.slice(l); | ||
return body; | ||
} | ||
class Conv {} | ||
_defineProperty(Conv, "cjkAndFullChars", text => { | ||
let tx = '', | ||
i = 0, | ||
t = '', | ||
l = text.length, | ||
n; | ||
while (i < l && (n = text.charCodeAt(i))) { | ||
t += n === 0x20 ? enumFullAngleChars.SP : 0x20 < n && n < 0x7f ? String.fromCharCode(n + DELTA_FULL) : text[i]; | ||
i++; | ||
} | ||
while (i < l && (n = text.charCodeAt(i++))) tx += n < 0xff00 ? CharConv.cjkPunc(n) : CharConv.fullChars(n); | ||
return t; | ||
}; | ||
const _halfToFull = function (tx) { | ||
const { | ||
ansi, | ||
lean | ||
} = this; | ||
if (ansi && charsetAnsi.hasAnsi(tx)) tx = charsetAnsi.clearAnsi(tx); | ||
if (lean) tx = tx.replace(LEAN_REG, (_, x) => x); | ||
return _halfToFullCore(tx); | ||
}; | ||
const halfToFull = (text, { | ||
ansi = true, | ||
lean = true | ||
} = {}) => _halfToFull.call({ | ||
ansi, | ||
lean | ||
}, text); | ||
const FullWidth = ({ | ||
ansi = true, | ||
lean = true | ||
} = {}) => _halfToFull.bind({ | ||
ansi, | ||
lean | ||
return tx; | ||
}); | ||
_defineProperty(Conv, "fullChars", text => { | ||
let tx = '', | ||
i = 0, | ||
l = text.length, | ||
n; | ||
while (i < l && (n = text.charCodeAt(i++))) tx += CharConv.fullChars(n); | ||
return tx; | ||
}); | ||
class CharConv { | ||
static cjkPunc(charCode) { | ||
if (charCode === 0x3000) return enumChars.SP; | ||
if (charCode === 0x3001) return enumChars.CO; | ||
if (charCode === 0x3002) return enumChars.DOT; | ||
if (charCode === 0x3010) return '['; | ||
if (charCode === 0x3011) return ']'; | ||
return String.fromCharCode(charCode); | ||
} | ||
static fullChars(charCode) { | ||
return String.fromCharCode(0xFF & charCode + 0x20); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {string} tx | ||
* @returns {boolean} | ||
* @param {boolean} [cjk=true] | ||
* @returns {any} | ||
* @constructor | ||
*/ | ||
const isNumeric = tx => REG_NUM_FULL.test(tx); | ||
const FullToHalf = ({ | ||
cjk = true | ||
} = {}) => { | ||
const conv = cjk ? Conv.cjkAndFullChars : Conv.fullChars; | ||
return _fullToHalfCore.bind({ | ||
conv | ||
}); | ||
}; | ||
/** | ||
* | ||
* @param {string} [text] | ||
* @param {boolean} [cjk=true] | ||
* @returns {string} | ||
*/ | ||
exports.FullWidth = FullWidth; | ||
exports.halfToFull = halfToFull; | ||
const fullToHalf = (text, { | ||
cjk = true | ||
} = {}) => { | ||
const conv = cjk ? Conv.cjkAndFullChars : Conv.fullChars; | ||
return _fullToHalfCore.call({ | ||
conv | ||
}, text); | ||
}; | ||
exports.FullToHalf = FullToHalf; | ||
exports.fullToHalf = fullToHalf; | ||
exports.hasFull = hasFull; | ||
exports.isNumeric = isNumeric; |
@@ -1,3 +0,2 @@ | ||
import { hasAnsi, clearAnsi } from '@texting/charset-ansi'; | ||
import { SP } from '@texting/enum-full-angle-chars'; | ||
import { SP, CO, DOT } from '@texting/enum-chars'; | ||
@@ -22,12 +21,32 @@ const CJK_PUNCS = '\u3000-\u303f'; | ||
const DELTA_FULL = 0xfee0; // export const REG_NUM_FULL = /^\s*[-+]?(?:,*[0-9]+)*.?[0-9]+\s*$/ | ||
const REG_FULL = new RegExp(`[${CJK_PUNCS}${FULL_CHARS}]+`, 'g'); // /[\uff01-\uff5e|\u3000]+/g | ||
const REG_NUM_FULL = new RegExp(`^\s*[-+]?(?:,*[${FULL_NUM}]+)*.?[${FULL_NUM}]+\s*$`); | ||
const LEAN_REG = /(\W)\s+/g; | ||
/** | ||
* Half-angle string -> Full-angle string | ||
* 半角转化为全角 | ||
* a.全角空格为12288,半角空格为32 | ||
* b.其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 | ||
* | ||
* @param {string} tx | ||
* @returns {boolean} | ||
*/ | ||
const isNumeric = tx => REG_NUM_FULL.test(tx); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Full-angle string -> Half-angle string | ||
* 全角转换为半角 | ||
* @param {string} text | ||
@@ -38,47 +57,93 @@ * @returns {string} | ||
const _halfToFullCore = text => { | ||
let l = text === null || text === void 0 ? void 0 : text.length, | ||
function _fullToHalfCore(text) { | ||
const { | ||
conv | ||
} = this; | ||
let ms, | ||
l = 0, | ||
r = 0, | ||
sp, | ||
ph, | ||
body = ''; | ||
while ((ms = REG_FULL.exec(text)) && ([ph] = ms)) { | ||
r = ms.index; | ||
if (l !== r && (sp = text.slice(l, r))) body += sp; | ||
body += conv(ph); | ||
l = REG_FULL.lastIndex; | ||
} | ||
if (l < (text === null || text === void 0 ? void 0 : text.length)) body += text.slice(l); | ||
return body; | ||
} | ||
class Conv {} | ||
_defineProperty(Conv, "cjkAndFullChars", text => { | ||
let tx = '', | ||
i = 0, | ||
t = '', | ||
l = text.length, | ||
n; | ||
while (i < l && (n = text.charCodeAt(i))) { | ||
t += n === 0x20 ? SP : 0x20 < n && n < 0x7f ? String.fromCharCode(n + DELTA_FULL) : text[i]; | ||
i++; | ||
} | ||
while (i < l && (n = text.charCodeAt(i++))) tx += n < 0xff00 ? CharConv.cjkPunc(n) : CharConv.fullChars(n); | ||
return t; | ||
}; | ||
const _halfToFull = function (tx) { | ||
const { | ||
ansi, | ||
lean | ||
} = this; | ||
if (ansi && hasAnsi(tx)) tx = clearAnsi(tx); | ||
if (lean) tx = tx.replace(LEAN_REG, (_, x) => x); | ||
return _halfToFullCore(tx); | ||
}; | ||
const halfToFull = (text, { | ||
ansi = true, | ||
lean = true | ||
} = {}) => _halfToFull.call({ | ||
ansi, | ||
lean | ||
}, text); | ||
const FullWidth = ({ | ||
ansi = true, | ||
lean = true | ||
} = {}) => _halfToFull.bind({ | ||
ansi, | ||
lean | ||
return tx; | ||
}); | ||
_defineProperty(Conv, "fullChars", text => { | ||
let tx = '', | ||
i = 0, | ||
l = text.length, | ||
n; | ||
while (i < l && (n = text.charCodeAt(i++))) tx += CharConv.fullChars(n); | ||
return tx; | ||
}); | ||
class CharConv { | ||
static cjkPunc(charCode) { | ||
if (charCode === 0x3000) return SP; | ||
if (charCode === 0x3001) return CO; | ||
if (charCode === 0x3002) return DOT; | ||
if (charCode === 0x3010) return '['; | ||
if (charCode === 0x3011) return ']'; | ||
return String.fromCharCode(charCode); | ||
} | ||
static fullChars(charCode) { | ||
return String.fromCharCode(0xFF & charCode + 0x20); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {string} tx | ||
* @returns {boolean} | ||
* @param {boolean} [cjk=true] | ||
* @returns {any} | ||
* @constructor | ||
*/ | ||
const isNumeric = tx => REG_NUM_FULL.test(tx); | ||
const FullToHalf = ({ | ||
cjk = true | ||
} = {}) => { | ||
const conv = cjk ? Conv.cjkAndFullChars : Conv.fullChars; | ||
return _fullToHalfCore.bind({ | ||
conv | ||
}); | ||
}; | ||
/** | ||
* | ||
* @param {string} [text] | ||
* @param {boolean} [cjk=true] | ||
* @returns {string} | ||
*/ | ||
export { FullWidth, halfToFull, hasFull, isNumeric }; | ||
const fullToHalf = (text, { | ||
cjk = true | ||
} = {}) => { | ||
const conv = cjk ? Conv.cjkAndFullChars : Conv.fullChars; | ||
return _fullToHalfCore.call({ | ||
conv | ||
}, text); | ||
}; | ||
export { FullToHalf, fullToHalf, hasFull, isNumeric }; |
{ | ||
"name": "@texting/charset-fullwidth", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Convert between half-width and full-width", | ||
@@ -18,6 +18,6 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@texting/charset-ansi": "^0.0.1", | ||
"@texting/enum-chars": "^0.0.1", | ||
"@texting/enum-full-angle-chars": "^0.0.1", | ||
"@texting/lange": "^0.0.1" | ||
"@texting/charset-ansi": "^0.0.2", | ||
"@texting/enum-chars": "^0.0.2", | ||
"@texting/enum-chars-fullwidth": "^0.0.2", | ||
"@texting/lange": "^0.0.2" | ||
}, | ||
@@ -40,3 +40,3 @@ "repository": { | ||
"homepage": "https://github.com/gadge/spare#readme", | ||
"gitHead": "59f3247c566b34b21cac95100e4802ff6e046299" | ||
"gitHead": "fe8ad527c1de5be3a998d3a398f8638c65f3734b" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10019
247
1
+ Added@texting/charset-ansi@0.0.2(transitive)
+ Added@texting/enum-chars@0.0.2(transitive)
+ Added@texting/enum-chars-fullwidth@0.0.2(transitive)
+ Added@texting/lange@0.0.2(transitive)
+ Added@texting/regex-charset@0.0.2(transitive)
- Removed@texting/charset-ansi@0.0.1(transitive)
- Removed@texting/enum-chars@0.0.1(transitive)
- Removed@texting/enum-full-angle-chars@0.0.1(transitive)
- Removed@texting/lange@0.0.1(transitive)
- Removed@texting/regex-charset@0.0.1(transitive)
Updated@texting/charset-ansi@^0.0.2
Updated@texting/enum-chars@^0.0.2
Updated@texting/lange@^0.0.2