Skip to content

Commit

Permalink
Refactor JS Code + Add support for Deno.
Browse files Browse the repository at this point in the history
  • Loading branch information
NCPlayz committed Jan 23, 2021
1 parent 22dab12 commit 16378ce
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 140 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
## Installation

### Node:

```sh
$ npm i bottomify
# OR
$ yarn add bottomify
```

### Deno:

```ts
import { encode, decode } from "https://deno.land/x/[email protected]/deno.ts"
```

### Browser:

```html
<!-- unpkg -->
<script src="https://unpkg.com/[email protected]/dist/bottomify.js"></script>
Expand All @@ -29,7 +37,5 @@ $ yarn add bottomify
## Examples

```js
const bottomify = require("bottomify")

console.log(bottomify.encode("Hello World!"))
console.log(encode("Hello World!"));
```
117 changes: 44 additions & 73 deletions dist/bottomify.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
"use strict";
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
Expand All @@ -31,77 +20,59 @@ var __spread = (this && this.__spread) || function () {
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var CHARACTER_VALUES = {
200: "🫂",
50: "💖",
10: "✨",
5: "🥺",
1: ",",
0: "❤️",
};
var CHARACTER_VALUES_MAP = Object.entries(CHARACTER_VALUES).reverse();
var VALUES_CHARACTER = CHARACTER_VALUES_MAP.reduce(function (obj, item) { return (obj[item[1]] = item[0]) && obj; }, {});
var CHARACTER_VALUES = [
[200, "🫂"],
[50, "💖"],
[10, "✨"],
[5, "🥺"],
[1, ","],
[0, "❤️"],
];
var SECTION_SEPERATOR = "👉👈";
var FINAL_TERMINATOR = new RegExp("(" + SECTION_SEPERATOR + ")?$");
function encode(value) {
var e_1, _a, e_2, _b;
var input = Array.from(value).map(function (v) { return v.codePointAt(0); });
var output = [];
function textEncoder() {
try {
for (var input_1 = __values(input), input_1_1 = input_1.next(); !input_1_1.done; input_1_1 = input_1.next()) {
var char = input_1_1.value;
while (char !== 0) {
try {
for (var CHARACTER_VALUES_MAP_1 = (e_2 = void 0, __values(CHARACTER_VALUES_MAP)), CHARACTER_VALUES_MAP_1_1 = CHARACTER_VALUES_MAP_1.next(); !CHARACTER_VALUES_MAP_1_1.done; CHARACTER_VALUES_MAP_1_1 = CHARACTER_VALUES_MAP_1.next()) {
var _c = __read(CHARACTER_VALUES_MAP_1_1.value, 2), value_1 = _c[0], emoji = _c[1];
var parsedValue = parseInt(value_1);
if (char >= parsedValue) {
char -= parsedValue;
output.push(emoji);
break;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (CHARACTER_VALUES_MAP_1_1 && !CHARACTER_VALUES_MAP_1_1.done && (_b = CHARACTER_VALUES_MAP_1.return)) _b.call(CHARACTER_VALUES_MAP_1);
}
finally { if (e_2) throw e_2.error; }
}
}
output.push(SECTION_SEPERATOR);
}
return new TextEncoder();
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (input_1_1 && !input_1_1.done && (_a = input_1.return)) _a.call(input_1);
}
finally { if (e_1) throw e_1.error; }
catch (_a) {
// more than likely Node.JS
return new (require("util").TextEncoder)();
}
return output.join("");
}
function encodeChar(charValue) {
if (charValue === 0)
return "";
var _a = __read(CHARACTER_VALUES.find(function (_a) {
var _b = __read(_a, 1), val = _b[0];
return charValue >= val;
}) ||
CHARACTER_VALUES.find(function () { return 0; }), 2), val = _a[0], currentCase = _a[1];
return "" + currentCase + encodeChar(charValue - val);
}
function encode(value) {
return Array.from(textEncoder().encode(value))
.map(function (v) { return encodeChar(v) + SECTION_SEPERATOR; })
.join("");
}
exports.encode = encode;
function decode(value) {
var input = value
return String.fromCodePoint.apply(String, __spread(value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR);
var output = [];
if (Array.from(value).some(function (v) {
return !(Object.keys(VALUES_CHARACTER).includes(v) ||
SECTION_SEPERATOR.includes(v));
})) {
throw TypeError("Invalid bottom text: '" + value + "'");
}
input.forEach(function (word) {
var codepoint = 0;
Array.from(word).forEach(function (char) {
codepoint += parseInt(VALUES_CHARACTER[char]);
});
output.push(codepoint);
});
return String.fromCodePoint.apply(String, __spread(output));
.split(SECTION_SEPERATOR)
.map(function (letters) {
return Array.from(letters)
.map(function (character) {
var _a = __read(CHARACTER_VALUES.find(function (_a) {
var _b = __read(_a, 2), _ = _b[0], em = _b[1];
return em == character;
}), 2), value = _a[0], emoji = _a[1];
if (!emoji) {
throw TypeError("Invalid bottom text: '" + value + "'");
}
return value;
})
.reduce(function (p, c) { return p + c; });
})));
}
exports.decode = decode;
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.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bottomify",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/bottomify.js",
"unpkg": "dist/bottomify.min.js",
"types": "dist/bottomify.d.ts",
Expand All @@ -16,5 +16,8 @@
},
"files": [
"dist/*"
]
],
"dependencies": {
"yargs": "^16.2.0"
}
}
105 changes: 49 additions & 56 deletions src/bottomify.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,59 @@
const CHARACTER_VALUES = {
200: "🫂",
50: "💖",
10: "✨",
5: "🥺",
1: ",",
0: "❤️",
};
const CHARACTER_VALUES_MAP = Object.entries(CHARACTER_VALUES).reverse();
const VALUES_CHARACTER = CHARACTER_VALUES_MAP.reduce(
(obj, item) => (obj[item[1]] = item[0]) && obj,
{}
);
const CHARACTER_VALUES: [number, string][] = [
[200, "🫂"],
[50, "💖"],
[10, "✨"],
[5, "🥺"],
[1, ","],
[0, "❤️"],
];
const SECTION_SEPERATOR = "👉👈";
const FINAL_TERMINATOR = new RegExp(`(${SECTION_SEPERATOR})?$`);

export function encode(value: string): string {
let input = Array.from(value).map((v) => v.codePointAt(0));
let output = [];
interface TextEncoderType {
encode: (input?: string) => Uint8Array
}

for (let char of input) {
while (char !== 0) {
for (let [value, emoji] of CHARACTER_VALUES_MAP) {
let parsedValue = parseInt(value);
if (char >= parsedValue) {
char -= parsedValue;
output.push(emoji);
break;
}
}
}
output.push(SECTION_SEPERATOR);
function textEncoder(): TextEncoderType {
try {
return new TextEncoder();
} catch {
// more than likely Node.JS
return new (require("util").TextEncoder)();
}

return output.join("");
}

export function decode(value: string): string {
let input: string[] = value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR);
let output = [];

if (
Array.from(value).some(
(v) =>
!(
Object.keys(VALUES_CHARACTER).includes(v) ||
SECTION_SEPERATOR.includes(v)
)
)
) {
throw TypeError(`Invalid bottom text: '${value}'`);
}
function encodeChar(charValue: number): string {
if (charValue === 0) return "";
let [val, currentCase]: [number, string] =
CHARACTER_VALUES.find(([val]) => charValue >= val) ||
CHARACTER_VALUES.find(() => 0);
return `${currentCase}${encodeChar(charValue - val)}`;
}

input.forEach((word) => {
let codepoint = 0;
Array.from(word).forEach((char) => {
codepoint += parseInt(VALUES_CHARACTER[char]);
});
output.push(codepoint);
});
export function encode(value: string): string {
return Array.from(textEncoder().encode(value))
.map((v: number) => encodeChar(v) + SECTION_SEPERATOR)
.join("");
}

return String.fromCodePoint(...output);
export function decode(value: string): string {
return String.fromCodePoint(
...value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR)
.map((letters) => {
return Array.from(letters)
.map((character) => {
let [value, emoji]: [number, string] = CHARACTER_VALUES.find(
([_, em]) => em == character
);
if (!emoji) {
throw TypeError(`Invalid bottom text: '${value}'`);
}
return value;
})
.reduce((p, c) => p + c);
})
);
}
53 changes: 53 additions & 0 deletions src/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const CHARACTER_VALUES: [number, string][] = [
[200, "🫂"],
[50, "💖"],
[10, "✨"],
[5, "🥺"],
[1, ","],
[0, "❤️"],
];
const SECTION_SEPERATOR = "👉👈";
const FINAL_TERMINATOR = new RegExp(`(${SECTION_SEPERATOR})?$`);

interface TextEncoderType {
encode: (input?: string) => Uint8Array;
}

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

function encodeChar(charValue: number): string {
if (charValue === 0) return "";
let [val, currentCase]: [number, string] =
CHARACTER_VALUES.find(([val]) => charValue >= val) || CHARACTER_VALUES[-1];
return `${currentCase}${encodeChar(charValue - val)}`;
}

export function encode(value: string): string {
return Array.from(textEncoder().encode(value))
.map((v: number) => encodeChar(v) + SECTION_SEPERATOR)
.join("");
}

export function decode(value: string): string {
return String.fromCodePoint(
...value
.trim()
.replace(FINAL_TERMINATOR, "")
.split(SECTION_SEPERATOR)
.map((letters) => {
return Array.from(letters)
.map((character) => {
let [value, emoji]: [number, string] =
CHARACTER_VALUES.find(([_, em]) => em == character) ||
CHARACTER_VALUES[-1];
if (!emoji) {
throw TypeError(`Invalid bottom text: '${value}'`);
}
return value;
})
.reduce((p, c) => p + c);
})
);
}
7 changes: 2 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
"outDir": "dist",
"esModuleInterop": true,
"downlevelIteration": true,
"declaration": true,
"declaration": true
},
"exclude": [
"test/**/*.ts",
"dist"
]
"exclude": ["test/**/*.ts", "dist", "src/deno.ts"]
}
Loading

0 comments on commit 16378ce

Please sign in to comment.