Skip to content

Commit

Permalink
fix decoding of non-ascii characters
Browse files Browse the repository at this point in the history
  • Loading branch information
och-och committed Mar 18, 2021
1 parent aa46e07 commit ccc7f41
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
16 changes: 11 additions & 5 deletions dist/bottomify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ var __read = (this && this.__read) || function (o, n) {
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decode = exports.encode = void 0;
var CHARACTER_VALUES = [
[200, "🫂"],
[50, "💖"],
Expand All @@ -39,6 +36,15 @@ function textEncoder() {
return new (require("util").TextEncoder)();
}
}
function textDecoder() {
try {
return new TextDecoder();
}
catch (_a) {
// more than likely Node.JS
return new (require("util").TextDecoder)();
}
}
function encodeChar(charValue) {
if (charValue === 0)
return "";
Expand All @@ -55,7 +61,7 @@ function encode(value) {
}
exports.encode = encode;
function decode(value) {
return String.fromCodePoint.apply(String, __spread(value
return textDecoder().decode(Uint8Array.from(value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR)
Expand Down
2 changes: 1 addition & 1 deletion dist/bottomify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions dist/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
19 changes: 16 additions & 3 deletions src/bottomify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ interface TextEncoderType {
encode: (input?: string) => Uint8Array;
}

interface TextDecoderType {
decode: (input?: Uint8Array) => string;
}

function textEncoder(): TextEncoderType {
try {
return new TextEncoder();
Expand All @@ -22,6 +26,15 @@ function textEncoder(): TextEncoderType {
}
}

function textDecoder(): TextDecoderType {
try {
return new TextDecoder();
} catch {
// more than likely Node.JS
return new (require("util").TextDecoder)();
}
}

function encodeChar(charValue: number): string {
if (charValue === 0) return "";
let [val, currentCase]: [number, string] =
Expand All @@ -36,8 +49,8 @@ export function encode(value: string): string {
}

export function decode(value: string): string {
return String.fromCodePoint(
...value
return textDecoder().decode(Uint8Array.from(
value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR)
Expand All @@ -54,5 +67,5 @@ export function decode(value: string): string {
})
.reduce((p, c) => p + c);
})
);
));
}
14 changes: 11 additions & 3 deletions src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ interface TextEncoderType {
encode: (input?: string) => Uint8Array;
}

interface TextDecoderType {
decode: (input?: Uint8Array) => string;
}

function textEncoder(): TextEncoderType {
return new TextEncoder();
}

function textDecoder(): TextDecoderType {
return new TextDecoder();
}

function encodeChar(charValue: number): string {
if (charValue === 0) return "";
let [val, currentCase]: [number, string] =
Expand All @@ -31,8 +39,8 @@ export function encode(value: string): string {
}

export function decode(value: string): string {
return String.fromCodePoint(
...value
return textDecoder().decode(Uint8Array.from(
value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR)
Expand All @@ -49,5 +57,5 @@ export function decode(value: string): string {
})
.reduce((p, c) => p + c);
})
);
));
}

0 comments on commit ccc7f41

Please sign in to comment.