Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NCPlayz committed Jan 21, 2021
0 parents commit 22dab12
Show file tree
Hide file tree
Showing 12 changed files with 1,914 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Empty file added .npmignore
Empty file.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Bottomify

![npm bundle size](https://img.shields.io/bundlephobia/min/bottomify?style=plastic)

## Installation

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

### Browser:
```html
<!-- unpkg -->
<script src="https://unpkg.com/[email protected]/dist/bottomify.js"></script>
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bottomify.js"></script>

<!-- Minified -->

<!-- unpkg -->
<script src="https://unpkg.com/[email protected]/dist/bottomify.min.js"></script>
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bottomify.min.js"></script>
```

## Examples

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

console.log(bottomify.encode("Hello World!"))
```
2 changes: 2 additions & 0 deletions dist/bottomify.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare function encode(value: string): string;
export declare function decode(value: string): string;
107 changes: 107 additions & 0 deletions dist/bottomify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"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;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
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 });
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 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 = [];
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);
}
}
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; }
}
return output.join("");
}
exports.encode = encode;
function decode(value) {
var input = 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));
}
exports.decode = decode;
1 change: 1 addition & 0 deletions dist/bottomify.min.js

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

20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "bottomify",
"version": "0.1.0",
"main": "dist/bottomify.js",
"unpkg": "dist/bottomify.min.js",
"types": "dist/bottomify.d.ts",
"repository": "https://github.com/bottom-software-foundation/bottom-js",
"license": "MIT",
"scripts": {
"build": "tsc & node-minify --input dist/bottomify.js --output dist/bottomify.min.js"
},
"devDependencies": {
"@types/node": "^14.14.22",
"node-minify": "^3.6.0",
"prettier": "^2.2.1"
},
"files": [
"dist/*"
]
}
66 changes: 66 additions & 0 deletions src/bottomify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 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 = [];

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);
}

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}'`);
}

input.forEach((word) => {
let codepoint = 0;
Array.from(word).forEach((char) => {
codepoint += parseInt(VALUES_CHARACTER[char]);
});
output.push(codepoint);
});

return String.fromCodePoint(...output);
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true,
"downlevelIteration": true,
"declaration": true,
},
"exclude": [
"test/**/*.ts",
"dist"
]
}
Loading

0 comments on commit 22dab12

Please sign in to comment.