Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ type OmitThisParameterType<T> = T extends (this: never, ...args: infer A) => inf

type ThisType<T> = T;

// The options toLocaleString takes (Intl's number and date-time options, the ones in common use).
// Taken so such calls compile; there is no locale data to apply them with.
interface ToLocaleStringOptions {
localeMatcher?: string;
style?: string;
currency?: string;
currencyDisplay?: string;
unit?: string;
useGrouping?: boolean;
minimumIntegerDigits?: number;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
timeZone?: string;
hour12?: boolean;
}

function __is<V extends T, T>(t: T): t is V {
return true;
}
Expand Down Expand Up @@ -164,6 +180,12 @@ namespace __Array {
return this.join();
}

// the elements' text joined by ",". The locale and its options are taken, as JavaScript takes
// them, but not applied: there is no locale data, so it is the text toString gives.
function toLocaleString<T>(this: T[], locales?: string | string[], options?: ToLocaleStringOptions) {
return this.join();
}

// A callback takes the element and its index, as JavaScript gives them; one that takes the element only
// is given as one taking both (the compiler's fewer-parameters thunk).
function every<T>(this: T[], func: (v: T, i: number) => boolean) {
Expand Down Expand Up @@ -1163,7 +1185,8 @@ class TypedArray<T> {
return this.join();
}

public toLocaleString() {
// the locale and its options are taken but not applied, as for an array
public toLocaleString(locales?: string | string[], options?: ToLocaleStringOptions) {
return this.join();
}

Expand Down Expand Up @@ -1562,7 +1585,8 @@ class ConvertingTypedArray<S> {
return this.join();
}

public toLocaleString() {
// the locale and its options are taken but not applied, as for an array
public toLocaleString(locales?: string | string[], options?: ToLocaleStringOptions) {
return this.join();
}

Expand Down Expand Up @@ -2824,13 +2848,20 @@ class TextDecoder<T = u8> {
// and these are the functions over such values. A rejected promise is a thrown exception. There is
// no `then`: a value has no methods. Each is generic, as `isLockFree` is above.
namespace Promise {
function resolve<T>(value: T): T {
// with no value: a settled promise of nothing, for `await Promise.resolve()`
function resolve<T = undefined>(value?: T): T {
return value;
}

// throws the reason: what awaiting a rejected promise does. The reason keeps its own type, so a
// `catch (e: Error)` catches an Error.
function reject<T = never, R = Error>(reason: R): T {
// `catch (e: Error)` catches an Error. With no reason it throws undefined, as JavaScript does
// (in an any: undefined itself can't be thrown).
function reject<T = never, R = Error>(reason?: R): T {
if (reason === undefined) {
const nothing: any = undefined;
throw nothing;
}

throw reason;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ declare namespace __String {

function search(this: string, regexp: RegExp): int;

function slice(this: string, indexStart: int, indexEnd?: int): string;
function slice(this: string, indexStart?: int, indexEnd?: int): string;

function split(this: string, separator?: string | RegExp, limit?: int): string[];

Expand Down Expand Up @@ -441,7 +441,7 @@ declare class String {

search(this: string, regexp: RegExp): int;

slice(this: string, indexStart: int, indexEnd?: int): string;
slice(this: string, indexStart?: int, indexEnd?: int): string;

split(this: string, separator?: string | RegExp, limit?: int): string[];

Expand Down
4 changes: 2 additions & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ namespace __String {
return regexp.search(this);
}

export function slice(this: string, indexStart: int, indexEnd: int = this.length): string {
export function slice(this: string, indexStart: int = 0, indexEnd: int = this.length): string {

const len: int = this.length;
if (indexStart < 0) {
Expand Down Expand Up @@ -1539,7 +1539,7 @@ export class String {
return this.value.search(regexp);
}

public slice(indexStart: int, indexEnd?: int): string {
public slice(indexStart?: int, indexEnd?: int): string {
return this.value.slice(indexStart, indexEnd);
}

Expand Down
38 changes: 38 additions & 0 deletions src/types/lib.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,42 @@ interface ClassIterator<T> {
next(): IterateResult<T>;
}

// TypeScript's iteration types, as far as a generator here can be one. A generator function returns
// an object with next(), and for...of takes any object with next(), so here an Iterable is such an
// object too (not one with [Symbol.iterator]): `function* g(): Generator<number>`, and a generator
// given to an Iterable<T> or Iterator<T> parameter, work. TReturn and TNext are taken but not used:
// next() takes no value, and a returned value is one more value. IteratorResult is an alias, not an
// interface, so a generator's next() matches it.
// Not yet (compiler): an array or a string as an Iterable<T>, next(value), return() and throw().
type IteratorResult<T, TReturn = any> = IterateResult<T>;

interface Iterable<T> {
next: () => IterateResult<T>;
}

interface IterableIterator<T> {
next: () => IterateResult<T>;
}

interface Generator<T = unknown, TReturn = any, TNext = any> {
next: () => IterateResult<T>;
}

// Promise<T> is T: an async generator's next() gives the result itself
interface AsyncIterator<T> {
next: () => Promise<IteratorResult<T>>;
}

interface AsyncIterable<T> {
next: () => Promise<IteratorResult<T>>;
}

interface AsyncIterableIterator<T> {
next: () => Promise<IteratorResult<T>>;
}

interface AsyncGenerator<T = unknown, TReturn = any, TNext = any> {
next: () => Promise<IteratorResult<T>>;
}

type time_t = long;
24 changes: 24 additions & 0 deletions tests/array_toLocaleString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// toLocaleString takes a locale and options, as in JavaScript; with no locale data it gives the
// text toString gives.

function main() {
const a = [1, 2, 3];
assert(a.toLocaleString() == "1,2,3", "array toLocaleString()");
assert(a.toLocaleString("en-US") == "1,2,3", "array toLocaleString(locale)");
assert(a.toLocaleString("en-US", { style: "currency", currency: "EUR" }) == "1,2,3", "array toLocaleString(locale, options)");
assert(a.toLocaleString(["en-US", "fr"]) == "1,2,3", "array toLocaleString(locales)");

const s = ["x" + 1, "y"];
assert(s.toLocaleString("fr", { timeZone: "UTC" }) == "x1,y", "string array");

const i8 = new Int8Array(3);
i8[1] = 5;
assert(i8.toLocaleString() == "0,5,0", "Int8Array toLocaleString()");
assert(i8.toLocaleString("en-US", { minimumFractionDigits: 2 }) == "0,5,0", "Int8Array toLocaleString(locale, options)");

const c = new Uint8ClampedArray(2);
c[0] = 300;
assert(c.toLocaleString("en-US") == "255,0", "Uint8ClampedArray toLocaleString(locale)");

print("ALL DONE");
}
70 changes: 70 additions & 0 deletions tests/iteration_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// TypeScript's iteration type names: a generator function can be declared to return a Generator,
// IterableIterator or Iterator, an async one an AsyncGenerator, and a generator is given to
// Iterable / AsyncIterable parameters (here an Iterable is an object with next(), as for...of takes).
// Number elements: under -mm=own, a generator returned as an interface of owned elements is not
// followed yet (a compiler limit, as for any interface declared outside the library).

function* numbers(): Generator<number> {
yield 1.5;
yield 2;
}

function* counted(n: int): IterableIterator<number> {
// a number: the declared element type does not convert a yielded int yet (compiler)
for (let i: number = 0; i < n; i++) {
yield i;
}
}

function* typed(): Generator<number, void, undefined> {
yield 4;
yield 5.5;
}

async function* later(): AsyncGenerator<number> {
yield 0.5;
yield 1;
}

function sum(values: Iterable<number>) {
let s: number = 0;
for (const v of values) {
s += v;
}

return s;
}

function sumOf(it: Iterator<number>) {
let s: number = 0;
for (const v of it) {
s += v;
}

return s;
}

async function sumLater(values: AsyncIterable<number>) {
let s: number = 0;
for await (const v of values) {
s += v;
}

return s;
}

function main() {
assert(sum(numbers()) == 3.5, "Generator as an Iterable");
assert(sum(counted(4)) == 6, "IterableIterator as an Iterable");
assert(sumOf(typed()) == 9.5, "Generator<T, TReturn, TNext> as an Iterator");

const it = numbers();
const first: IteratorResult<number> = it.next();
assert(first.value == 1.5 && !first.done, "IteratorResult");
it.next();
assert(it.next().done, "finished");

assert(await sumLater(later()) == 1.5, "AsyncGenerator as an AsyncIterable");

print("ALL DONE");
}
32 changes: 32 additions & 0 deletions tests/promise_no_args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Promise.resolve() and Promise.reject() with no argument: resolve gives a settled promise of
// nothing (void), reject throws undefined, as JavaScript rejects with undefined.

async function done(): Promise<void> {
await Promise.resolve();
}

function main() {
await Promise.resolve();
done();

let caught = false;
try {
Promise.reject();
} catch (e) {
caught = true;
}

assert(caught, "reject() throws");

let caughtTyped = false;
try {
const n: int = Promise.reject<int>();
print(n);
} catch (e) {
caughtTyped = true;
}

assert(caughtTyped, "reject<int>() throws");

print("ALL DONE");
}
18 changes: 18 additions & 0 deletions tests/string_slice_no_args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// slice() with no arguments copies the whole string: indexStart defaults to 0, as in JavaScript.

function main() {
const str = "abc" + 1;

assert(str.slice() == "abc1", "slice()");
assert(str.slice(undefined, 2) == "ab", "slice(undefined, 2)");
assert("".slice() == "", "empty slice()");

let x = str;
for (let i = 0; i < 3; i++) {
x = x.slice();
}

assert(x == "abc1", "slice() in a loop");

print("ALL DONE");
}
Loading