diff --git a/types/hogan.js/hogan.js-tests.ts b/types/hogan.js/hogan.js-tests.ts new file mode 100644 index 0000000000..3b47569adc --- /dev/null +++ b/types/hogan.js/hogan.js-tests.ts @@ -0,0 +1,24 @@ +import Hogan from "hogan.js"; + +// $ExpectType HoganTemplate +const compiled = Hogan.compile("{{#if}}{{value}}{{/if}}"); + +// $ExpectType string +Hogan.compile("<%_foo%><%value%><%/foo%>", { + asString: true, + sectionTags: [{ + o: '_foo', + c: 'foo' + }], + delimiters: '<% %>', + disableLambda: true +}); + +// $ExpectType string +compiled.render({ value: "test" }); + +// $ExpectType Token[] +const scanned = Hogan.scan("{{#if}}{{value}}{{/if}}"); + +// $ExpectType Leaf[] +Hogan.parse(scanned); diff --git a/types/hogan.js/index.d.ts b/types/hogan.js/index.d.ts new file mode 100644 index 0000000000..c9ede44cf4 --- /dev/null +++ b/types/hogan.js/index.d.ts @@ -0,0 +1,100 @@ +// Type definitions for hogan.js 3.0 +// Project: http://twitter.github.com/hogan.js/ +// Definitions by: Andrew Leedham +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +export interface Context { + [key: string]: any; +} + +export interface SectionTags { + o: string; + c: string; +} + +export interface HoganOptions { + asString?: boolean; + sectionTags?: SectionTags[]; + delimiters?: string; + disableLambda?: boolean; +} + +export interface Token { + tag: string; + otag?: string; + ctag?: string; + i?: number; + n?: string; + text?: string; +} + +export interface Leaf extends Token { + end: number; + nodes: Token[]; +} + +export type Tree = Leaf[]; + +export class HoganTemplate { + /** + * Renders the template to a string. + * + * @param context - The data to render the template with. + * @param partials - The partials to render the template with. + * @param indent - The string to indent when rendering the template. + * @returns A rendered template. + */ + render(context: Context, partials?: Partials, indent?: string): string; +} + +export interface Partials { + [symbol: string]: HoganTemplate; +} + +declare namespace Hogan { + export { HoganTemplate as Template, HoganTemplate as template }; + + export function compile( + text: string, + options?: HoganOptions & { asString: false } + ): HoganTemplate; + export function compile( + text: string, + options?: HoganOptions & { asString: true } + ): string; + /** + * Compiles templates to HoganTemplate objects, which have a render method. + * + * @param text - Raw mustache string to compile. + * @param options - Options to use when compiling. See https://github.com/twitter/hogan.js#compilation-options. + * @returns A HoganTemplate. + */ + export function compile( + text: string, + options?: HoganOptions + ): Template | string; + /** + * Scans templates returning an array of found tokens. + * + * @param text - Raw mustache string to scan. + * @param delimiters - A string that overrides the default delimiters. Example: "<% %>". + * @returns Found tokens. + */ + export function scan(text: string, delimiters?: string): Token[]; + /** + * Structures tokens into a tree. + * + * @param tokens - An array of scanned tokens. + * @param text - Unused pass undefined. + * @param options - Options to use when parsing. See https://github.com/twitter/hogan.js#compilation-options. + * @returns The tree structure of the given tokens. + */ + export function parse( + tokens: Token[], + text?: undefined, + options?: HoganOptions + ): Tree; +} + +export default Hogan; diff --git a/types/hogan.js/tsconfig.json b/types/hogan.js/tsconfig.json new file mode 100644 index 0000000000..cc84847867 --- /dev/null +++ b/types/hogan.js/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "hogan.js-tests.ts" + ] +} diff --git a/types/hogan.js/tslint.json b/types/hogan.js/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/hogan.js/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }