Skip to content

Commit

Permalink
Deploying to gh-pages from @ f010384 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Laux committed May 7, 2024
1 parent 39d7488 commit 734ae6b
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
17 changes: 17 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import init, {
parse_desktop_set,
parse_text,
get_first_emoji,
count_emojis_if_only_contains_emoji,
} from "./pkg/message_parser_wasm.js";

/** @typedef {import("./pkg/message_parser_wasm.js").ParsedElement} ParsedElement */
Expand Down Expand Up @@ -169,4 +171,19 @@ init().then(() => {
localStorage.setItem("lastMode", parse_mode.value);
action();
};

// emoji helpers
/** @type {HTMLInputElement} */
const emoji_input = document.getElementById("emoji-test");
const emoji_out_first = document.getElementById("emoji-test-first");
const emoji_out_count = document.getElementById("emoji-test-count");
const emoji_update = () => {
const text = emoji_input.value;
emoji_out_first.innerText = String(get_first_emoji(text));
emoji_out_count.innerText = String(
count_emojis_if_only_contains_emoji(text)
);
setTimeout(emoji_update, 1)
};
emoji_input.onchange = emoji_input.onkeydown = ()=>setTimeout(emoji_update, 1);
});
36 changes: 34 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@
border-radius: 2px;
padding: 2px;
}

table,
td,
tr {
border: 1px solid grey;
border-collapse: collapse;
}
td {
padding: 3px;
}
</style>
<h1>Message Parser</h1>
<h3>Input</h3>
<textarea
name="content"
id="input"
style="width: calc(100% - 10px)"
rows="10"
>Sample message.
>
Sample message.
#sampleHashtag
https://example.com/example
[email protected]
Expand All @@ -59,7 +71,8 @@ <h3>Input</h3>
print("And, of course")
print("A code block")
```
</textarea>
</textarea
>
<label for="parse_mode">Mode: </label>
<select name="parse_mode" id="parse_mode">
<option value="text">TEXT Only</option>
Expand Down Expand Up @@ -88,6 +101,25 @@ <h3>Output - AST</h3>
border: 1px solid lightgray;
"
></pre>

<h1>Emoji Helpers</h1>

<table>
<thead>
<tr>
<td>Input</td>
<td>First Emoji</td>
<td>Emoji count if only emojis</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" id="emoji-test" /></td>
<td id="emoji-test-first"></td>
<td id="emoji-test-count"></td>
</tr>
</tbody>
</table>
<script type="module" src="example.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ For usage in react you can look at how we integrated this package in deltachat-d
If you want to see it in action in deltachat-desktop, feel free to download it on <https://get.delta.chat>.
### Emoji Helper functions
```js
/** returns first emoji from text if text begins with an emoji */
export function get_first_emoji(input: string): string | undefined;
/** If string contains only emojis count the emojis otherwise retuns null */
export function count_emojis_if_only_contains_emoji(input: string): number | undefined;
```

### For Devs

#### 🛠️ Build with `wasm-pack build`
Expand Down
16 changes: 16 additions & 0 deletions pkg/message_parser_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ export function parse_text(s: string, enable_markdown: boolean): any;
* @returns {any}
*/
export function parse_desktop_set(s: string): any;
/**
* returns first emoji from text if text begins with an emoji
* @param {string} input
* @returns {string | undefined}
*/
export function get_first_emoji(input: string): string | undefined;
/**
* If string contains only emojis count the emojis otherwise retuns null
* @param {string} input
* @returns {number | undefined}
*/
export function count_emojis_if_only_contains_emoji(input: string): number | undefined;

export type PunycodeWarning = {
original_hostname: string;
Expand Down Expand Up @@ -49,8 +61,12 @@ export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly parse_text: (a: number, b: number, c: number) => number;
readonly parse_desktop_set: (a: number, b: number) => number;
readonly get_first_emoji: (a: number, b: number, c: number) => void;
readonly count_emojis_if_only_contains_emoji: (a: number, b: number, c: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
}

export type SyncInitInput = BufferSource | WebAssembly.Module;
Expand Down
43 changes: 43 additions & 0 deletions pkg/message_parser_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,49 @@ export function parse_desktop_set(s) {
return takeObject(ret);
}

/**
* returns first emoji from text if text begins with an emoji
* @param {string} input
* @returns {string | undefined}
*/
export function get_first_emoji(input) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.get_first_emoji(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
let v2;
if (r0 !== 0) {
v2 = getStringFromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
}
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

/**
* If string contains only emojis count the emojis otherwise retuns null
* @param {string} input
* @returns {number | undefined}
*/
export function count_emojis_if_only_contains_emoji(input) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.count_emojis_if_only_contains_emoji(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
return r0 === 0 ? undefined : r1 >>> 0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
Expand Down
Binary file modified pkg/message_parser_wasm_bg.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions pkg/message_parser_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
export const memory: WebAssembly.Memory;
export function parse_text(a: number, b: number, c: number): number;
export function parse_desktop_set(a: number, b: number): number;
export function get_first_emoji(a: number, b: number, c: number): void;
export function count_emojis_if_only_contains_emoji(a: number, b: number, c: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;

0 comments on commit 734ae6b

Please sign in to comment.