Skip to content

Instantly share code, notes, and snippets.

View lokshunhung's full-sized avatar
🐱

LS Hung lokshunhung

🐱
  • Hong Kong
View GitHub Profile
@lokshunhung
lokshunhung / JavaScript-RegExp.js
Created August 2, 2020 09:00
JavaScript: String.prototype.match & RegExp.prototype.exec
// A scratchpad testing the differences between:
// - `String.prototype.match` (with & without `g` flag)
// - `RegExp.prototype.exec` (with & without `g` flag)
const str = "A string\nWith 2 endlines\n";
(function () {
console.info("==== String.prototype.match GLOBAL ====");
const re = /(\w)(?<NamedGroup1>\w)\n/g;
// Returns an array containing **all** string segments matching the **whole regex**
@lokshunhung
lokshunhung / gist:7c350241f240c033f65144f2a25402f0
Last active January 22, 2021 09:44
JS/TS Types - Sharing 20200122
# 1) JS Types
There are 2 types in JS: **primitives** and **objects**.
## 1.1) Primitives
- `undefined`
- `null`
- `boolean`
- `number`
@lokshunhung
lokshunhung / animals.js
Last active May 18, 2021 02:49
ES5 prototype inheritance
function Animal(nickname, gender) {
this.nickname = nickname;
this.gender = gender;
}
Animal.prototype.getName = function () {
return this.nickname;
};
Animal.prototype.getGender = function () {
if (this.gender === "M") return "boy";
if (this.gender === "F") return "girl";
@lokshunhung
lokshunhung / good-types.md
Last active May 28, 2021 07:45
GoodTypes

Hackweek - GoodTypes

Good TypeScript, but without the "Script" part.

@lokshunhung
lokshunhung / validator-options.ts
Created July 5, 2021 02:42
class-validator options
const validatorOptions: import("class-validator").ValidatorOptions = {
//* do not include `target` and `value` when reporting error
validationError: { target: false, value: false },
//* unknown objects (aka not instance of decorated class) fails validation instead of passing
//* https://github.com/typestack/class-validator/blob/c2f999f9cc7ee2f40749780a89ae9f1f59f0b3e1/src/validation/ValidationExecutor.ts#L65
forbidUnknownValues: true,
//* strips non-decorated properties (aka "whitelisted") on the object to-be-validated
//* use `@Allow()` to "whitelist" a property if no decorators are appropriate
@lokshunhung
lokshunhung / libdispatch-efficiency-tips.md
Created December 9, 2022 13:35 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@lokshunhung
lokshunhung / file.swift
Last active July 11, 2023 09:54
Swift FileManager usage
import Foundation
@main struct FileScratch {
static func main() {
let fm: FileManager = .default
let home: URL = fm.homeDirectoryForCurrentUser
// Older API for `.appending(path:directoryHint:)`: `.appendingPathComponent(_:)`
// https://developer.apple.com/documentation/foundation/url/1780239-appendingpathcomponent
let outputFile = home
.appending(path: "Desktop")
@lokshunhung
lokshunhung / getTypeMetadata.swift
Created April 9, 2023 15:13
Swift 5 Type Metadata
import Foundation
// https://github.com/apple/swift/blob/main/include/swift/ABI/MetadataKind.def
struct MetadataKind: Equatable {
var rawValue: UInt
/// A class type.
static var `class`: Self {
.init(rawValue: 0) }
/// A struct type.
@lokshunhung
lokshunhung / consumeIterable.ts
Last active May 5, 2023 03:53
TS: consume iterable using generator `yield`s
// consumeIterable.ts
type NoInfer<T> = [T][T extends any ? 0 : never]; // https://github.com/millsp/ts-toolbelt/blob/319e551/sources/Function/NoInfer.ts#L27
type AnyIteratorResult = IteratorResult<any, any>;
function consumeIterable<T>(
producer: Iterable<T>,
consumer: () => Generator<undefined, void, NoInfer<T>>
): void {
const produce = producer[Symbol.iterator]();
import Combine
import RxSwift
public final class RxCurrentValueSubject<Output, Failure: Error>: Combine.Subject {
private let rxSubject: RxSwift.BehaviorSubject<Output>
private let combineSubject: Combine.CurrentValueSubject<Output, Failure>
private let disposeBag = RxSwift.DisposeBag()
public init(rxSubject: RxSwift.BehaviorSubject<Output>) {