-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JS Code + Add support for Deno.
- Loading branch information
Showing
8 changed files
with
269 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -29,7 +37,5 @@ $ yarn add bottomify | |
## Examples | ||
|
||
```js | ||
const bottomify = require("bottomify") | ||
|
||
console.log(bottomify.encode("Hello World!")) | ||
console.log(encode("Hello World!")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.