Good TypeScript, but without the "Script" part.
// 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** |
# 1) JS Types | |
There are 2 types in JS: **primitives** and **objects**. | |
## 1.1) Primitives | |
- `undefined` | |
- `null` | |
- `boolean` | |
- `number` |
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"; |
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 |
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
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") |
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. |
// 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>) { |