Skip to content

Instantly share code, notes, and snippets.

@seanghay
seanghay / khmer-zwsp-insert.js
Last active September 21, 2022 09:38
Automatically insert ZWSP into text.
import MagicString from "magic-string";
/**
* Automatically insert ZWSP into text.
* @param {string} value
* @param {string | undefined} sep
* @returns {string}
*/
export function insertZeroWidthSpace(value, sep = '\u200b') {
@seanghay
seanghay / Tiny JavaScript tokenizer.js
Created July 6, 2022 04:41 — forked from borgar/Tiny JavaScript tokenizer.js
A compact tokenizer written in JavaScript.
/*
* Tiny tokenizer
*
* - Accepts a subject string and an object of regular expressions for parsing
* - Returns an array of token objects
*
* tokenize('this is text.', { word:/\w+/, whitespace:/\s+/, punctuation:/[^\w\s]/ }, 'invalid');
* result => [{ token="this", type="word" },{ token=" ", type="whitespace" }, Object { token="is", type="word" }, ... ]
*
*/
@seanghay
seanghay / MaterialDialogFragment.kt
Created June 6, 2020 05:41
An alert dialog fragment that supports new material background.
/**
* Designed and developed by Seanghay Yath (@seanghay)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@seanghay
seanghay / canvas-grid-gap.md
Last active February 20, 2022 09:00
Draw Grid with Gap on Canvas

const segmenter = new Intl.Segmenter("km", {granularity: "word"});
const input = "នឹកបងឬនឹកគេ?";
const segments = segmenter.segment(input);
for (const {segment} of segments) {
console.log(segment)
}
// OUTPUT:
// នឹក
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
@seanghay
seanghay / Tests.kt
Created December 9, 2020 08:34
Format KH Phone Number
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import kotlin.jvm.Throws
class PhoneNumberSanitizerTest {
@Test
fun `COUNTRY_CODE should be +855`() {
assertEquals("+855", PhoneNumberSanitizer.COUNTRY_CODE)
@seanghay
seanghay / view-binding-ktx.kt
Last active October 26, 2020 03:22
ViewBinding Property Delegate with Android Lifecycle
import android.os.Looper
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
@seanghay
seanghay / HomeFragment.kt
Created October 25, 2020 10:01
ViewBinding inside Fragment
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding: FragmentHomeBinding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
@seanghay
seanghay / MainActivity.kt
Created October 25, 2020 09:54
The final result of ViewBinding KTX
class MainActivity : AppCompatActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)
}