dachev / node-cld

Language detection for Javascript (Node). Based on the CLD2 (Compact Language Detector) library from Google.
Apache License 2.0
316 stars 55 forks source link

Support definition for typescript user. #59

Closed webhacking closed 2 years ago

webhacking commented 5 years ago

This is my own creation. If there is no official position, I will post it myself.

interface Language {
  readonly name: string;
  readonly code: string;
  readonly percent: number;
  readonly score: number;
}
interface Chunk {
  readonly name: string;
  readonly code: string;
  readonly offset: number;
  readonly bytes: number;
}
interface Options {
  readonly isHTML: false;
  readonly languageHint: string;
  readonly encodingHint: string;
  readonly tldHint: string;
  readonly httpHint: string;
}
interface DetectLanguage {
  readonly reliable: boolean;
  readonly textBytes: number;
  readonly languages: Language[];
  readonly chunks: Chunk[];
}
export declare module 'cld' {
  declare function detect(text: string, options: Options, callback: (err: string, result: DetectLanguage) => {});
  declare function detect(text: string, callback: (err: string, result: DetectLanguage) => {});
}
mr-piratman commented 3 years ago

yo mate post it to @types/cld

thank you in any case :)

shtse8 commented 2 years ago

it saves my life! please post it to @types/cld thanks!

dachev commented 2 years ago

@shtse8 I've been out of the JS loop for some time now. Where do I have to put those definitions to make things work for you?

shtse8 commented 2 years ago

@shtse8 I've been out of the JS loop for some time now. Where do I have to put those definitions to make things work for you?

It's simple. add the .d.ts to the project and reference it in package.json. everything will be done while you publish your package again.

here is the reference: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

dachev commented 2 years ago

v2.8.0

dachev commented 2 years ago

Thank you @webhacking @shtse8 !

shtse8 commented 2 years ago

Thanks!!! cld is the best language detection package I have used.

I can use cld in typescript now with strict type supported! but it seems the definition not accuracy. it is missing async detect definition. only legacy.

please help to change: from

export declare module 'cld' {
  declare function detect(text: string, options: Options, callback: (err: string, result: DetectLanguage) => {});
  declare function detect(text: string, callback: (err: string, result: DetectLanguage) => {});
}

to

export declare module 'cld' {
  declare function detect(text: string, options: Options, callback: (err: string, result: DetectLanguage) => void): void;
  declare function detect(text: string, callback: (err: string, result: DetectLanguage) => void): void;
  declare function detect(text: string, options: Options): Promise<DetectLanguage>;
}
dachev commented 2 years ago

@shtse8 thank you. The new version is 2.8.1.