Skip to content

Instantly share code, notes, and snippets.

@cedx
Last active May 1, 2024 16:52
Show Gist options
  • Save cedx/e8934e60f58aa5b5debb4ec50a42bc1b to your computer and use it in GitHub Desktop.
Save cedx/e8934e60f58aa5b5debb4ec50a42bc1b to your computer and use it in GitHub Desktop.
JavaScript/JSDoc <=> TypeScript/TSDoc
/**
* Represents the front page or home URL transmitted when making requests.
*/
export class Blog {
/**
* The character encoding for the values included in comments.
* @type {string}
*/
charset;
/**
* The languages in use on the blog or site, in ISO 639-1 format.
* @type {string[]}
*/
languages;
/**
* The blog or site URL.
* @type {URL|null}
*/
url;
/**
* Creates a new blog.
* @param {Partial<BlogOptions>} options An object providing values to initialize this instance.
*/
constructor(options = {}) {
this.charset = options.charset ?? "";
this.languages = options.languages ?? [];
this.url = options.url ? new URL(options.url) : null;
}
/**
* Creates a new blog from the specified JSON object.
* @param {Record<string, any>} json A JSON object representing a blog.
* @returns {Blog} The instance corresponding to the specified JSON object.
*/
static fromJson(json) {
return new this({
charset: typeof json.blog_charset == "string" ? json.blog_charset : "",
languages: typeof json.blog_lang == "string" ? json.blog_lang.split(",").map(language => language.trim()) : [],
url: typeof json.blog == "string" ? json.blog : ""
});
}
/**
* Converts this object to a map in JSON format.
* @returns {Record<string, any>} The map in JSON format corresponding to this object.
*/
toJSON() {
/** @type {Record<string, any>} */
const map = {blog: this.url ? this.url.href : ""};
if (this.charset) map.blog_charset = this.charset;
if (this.languages.length) map.blog_lang = this.languages.join();
return map;
}
}
/**
* Represents the front page or home URL transmitted when making requests.
*/
export class Blog {
/**
* The character encoding for the values included in comments.
*/
charset: string;
/**
* The languages in use on the blog or site, in ISO 639-1 format.
*/
languages: string[];
/**
* The blog or site URL.
*/
url: URL|null;
/**
* Creates a new blog.
* @param options An object providing values to initialize this instance.
*/
constructor(options: Partial<BlogOptions>) {
this.charset = options.charset ?? "";
this.languages = options.languages ?? [];
this.url = options.url ? new URL(options.url) : null;
}
/**
* Creates a new blog from the specified JSON object.
* @param json A JSON object representing a blog.
* @returns The instance corresponding to the specified JSON object.
*/
static fromJson(json: Record<string, any>): Blog {
return new this({
charset: typeof json.blog_charset == "string" ? json.blog_charset : "",
languages: typeof json.blog_lang == "string" ? json.blog_lang.split(",").map(language => language.trim()) : [],
url: typeof json.blog == "string" ? json.blog : ""
});
}
/**
* Converts this object to a map in JSON format.
* @returns The map in JSON format corresponding to this object.
*/
toJSON(): Record<string, any> {
const map: Record<string, string> = {blog: this.url ? this.url.href : ""};
if (this.charset) map.blog_charset = this.charset;
if (this.languages.length) map.blog_lang = this.languages.join();
return map;
}
}
/**
* Specifies the result of a comment check.
* @enum {number}
*/
export const CheckResult = Object.freeze({
/**
* The comment is not a spam (i.e. a ham).
*/
ham: 0,
/**
* The comment is a spam.
*/
spam: 1,
/**
* The comment is a pervasive spam (i.e. it can be safely discarded).
*/
pervasiveSpam: 2
});
/**
* Specifies the result of a comment check.
*/
export enum CheckResult {
/**
* The comment is not a spam (i.e. a ham).
*/
ham,
/**
* The comment is a spam.
*/
spam,
/**
* The comment is a pervasive spam (i.e. it can be safely discarded).
*/
pervasiveSpam
}
/**
* Finds the instances of the specified command in the system path.
* @param {string} command The command to be resolved.
* @param {Partial<import("./finder.js").FinderOptions>} options The options to be passed to the finder.
* @returns {ResultSet} The search results.
*/
export function which(command, options = {}) {
return new ResultSet(command, new Finder(options));
}
/**
* Finds the instances of the specified command in the system path.
* @param command The command to be resolved.
* @param options The options to be passed to the finder.
* @returns The search results.
*/
export function which(command: string, options: Partial<FinderOptions> = {}): ResultSet {
return new ResultSet(command, new Finder(options));
}
/**
* Defines the options of an {@link Author} instance.
* @typedef {object} AuthorOptions
* @property {string} email The author's mail address. If you set it to `"akismet-guaranteed-spam@example.com"`, Akismet will always return `true`.
* @property {string} ipAddress The author's IP address.
* @property {string} name The author's name.
* @property {AuthorRole|string} role The author's role.
* @property {URL|string} url The URL of the author's website.
* @property {string} userAgent The author's user agent, that is the string identifying the Web browser used to submit comments.
*/
/**
* Defines the options of an {@link Author} instance.
*/
export interface AuthorOptions {
/**
* The author's mail address. If you set it to `"akismet-guaranteed-spam@example.com"`, Akismet will always return `true`.
*/
email: string;
/**
* The author's IP address.
*/
ipAddress: string;
/**
* The author's name.
*/
name: string;
/**
* The author's role.
*/
role: AuthorRole|string;
/**
* The URL of the author's website.
*/
url: URL|string;
/**
* The author's user agent, that is the string identifying the Web browser used to submit comments.
*/
userAgent: string;
}
{
"include": ["example/*.js", "src/**/*.js", "test/**/*.js"],
"compilerOptions": {
"baseUrl": ".",
"checkJs": true,
"esModuleInterop": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitOverride": true,
"resolveJsonModule": true,
"strict": true,
"target": "ESNext"
}
}
{
"include": ["example/*.ts", "src/**/*.ts", "test/**/*.ts"],
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitOverride": true,
"resolveJsonModule": true,
"strict": true,
"target": "ESNext"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment