diff --git a/types/crc/crc-tests.ts b/types/crc/crc-tests.ts index 36f5c023b6..7002635b11 100644 --- a/types/crc/crc-tests.ts +++ b/types/crc/crc-tests.ts @@ -1,5 +1,19 @@ -import * as crc from "crc"; -import * as fs from "fs"; +import * as fs from 'fs'; +import * as crc from 'crc'; + +import 'crc/crc91wire'; +import 'crc/crc17ccitt'; +import 'crc/crc17kermit'; +import 'crc/crc17modbus'; +import 'crc/crc17xmodem'; + +crc.default.default; // $ExpectError + +/** + * The `expectType` function from https://www.npmjs.com/package/tsd, + * except instead of returning `void`, it returns `T`. + */ +declare function expectType(t: T): T; // tests move from the readme of the module @@ -15,3 +29,33 @@ let value = crc.crc32('one'); value = crc.crc32('two', value); value = crc.crc32('three', value); value.toString(16); + +// `crc.*`: +expectType(expectType(crc.crc1)); +expectType(expectType(crc.crc8)); +expectType(expectType(crc.crc81wire)); + +expectType(expectType(crc.crc16)); +expectType(expectType(crc.crc16ccitt)); +expectType(expectType(crc.crc16kermit)); +expectType(expectType(crc.crc16modbus)); +expectType(expectType(crc.crc16xmodem)); + +expectType(expectType(crc.crc24)); +expectType(expectType(crc.crc32)); +expectType(expectType(crc.crcjam)); + +// `crc.default.*`: +expectType(expectType(crc.default.crc1)); +expectType(expectType(crc.default.crc8)); +expectType(expectType(crc.default.crc81wire)); + +expectType(expectType(crc.default.crc16)); +expectType(expectType(crc.default.crc16ccitt)); +expectType(expectType(crc.default.crc16kermit)); +expectType(expectType(crc.default.crc16modbus)); +expectType(expectType(crc.default.crc16xmodem)); + +expectType(expectType(crc.default.crc24)); +expectType(expectType(crc.default.crc32)); +expectType(expectType(crc.default.crcjam)); diff --git a/types/crc/crc1.d.ts b/types/crc/crc1.d.ts new file mode 100644 index 0000000000..a8e58c12dd --- /dev/null +++ b/types/crc/crc1.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc1'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc1(buf: CRCBufferSource, previous?: number): number; +declare namespace crc1 { + export { model, signed, crc1 as unsigned }; + export type { CRCBufferSource }; +} + +export default crc1; diff --git a/types/crc/crc16.d.ts b/types/crc/crc16.d.ts new file mode 100644 index 0000000000..2260ff9809 --- /dev/null +++ b/types/crc/crc16.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc-16'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc16(buf: CRCBufferSource, previous?: number): number; +declare namespace crc16 { + export { model, signed, crc16 as unsigned }; + export type { CRCBufferSource }; +} + +export default crc16; diff --git a/types/crc/crc16ccitt.d.ts b/types/crc/crc16ccitt.d.ts new file mode 100644 index 0000000000..9562fc326d --- /dev/null +++ b/types/crc/crc16ccitt.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'ccitt'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc16ccitt(buf: CRCBufferSource, previous?: number): number; +declare namespace crc16ccitt { + export { model, signed, crc16ccitt as unsigned }; + export type { CRCBufferSource }; +} + +export default crc16ccitt; diff --git a/types/crc/crc16kermit.d.ts b/types/crc/crc16kermit.d.ts new file mode 100644 index 0000000000..c1aa08fc62 --- /dev/null +++ b/types/crc/crc16kermit.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'kermit'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc16kermit(buf: CRCBufferSource, previous?: number): number; +declare namespace crc16kermit { + export { model, signed, crc16kermit as unsigned }; + export type { CRCBufferSource }; +} + +export default crc16kermit; diff --git a/types/crc/crc16modbus.d.ts b/types/crc/crc16modbus.d.ts new file mode 100644 index 0000000000..c231377a44 --- /dev/null +++ b/types/crc/crc16modbus.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc-16-modbus'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc16modbus(buf: CRCBufferSource, previous?: number): number; +declare namespace crc16modbus { + export { model, signed, crc16modbus as unsigned }; + export type { CRCBufferSource }; +} + +export default crc16modbus; diff --git a/types/crc/crc16xmodem.d.ts b/types/crc/crc16xmodem.d.ts new file mode 100644 index 0000000000..df5b79f1e0 --- /dev/null +++ b/types/crc/crc16xmodem.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'xmodem'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc16xmodem(buf: CRCBufferSource, previous?: number): number; +declare namespace crc16xmodem { + export { model, signed, crc16xmodem as unsigned }; + export type { CRCBufferSource }; +} + +export default crc16xmodem; diff --git a/types/crc/crc17ccitt.d.ts b/types/crc/crc17ccitt.d.ts new file mode 100644 index 0000000000..ea763440fd --- /dev/null +++ b/types/crc/crc17ccitt.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'ccitt'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc17ccitt(buf: CRCBufferSource, previous?: number): number; +declare namespace crc17ccitt { + export { model, signed, crc17ccitt as unsigned }; + export type { CRCBufferSource }; +} + +export default crc17ccitt; diff --git a/types/crc/crc17kermit.d.ts b/types/crc/crc17kermit.d.ts new file mode 100644 index 0000000000..f3984a8be4 --- /dev/null +++ b/types/crc/crc17kermit.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'kermit'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc17kermit(buf: CRCBufferSource, previous?: number): number; +declare namespace crc17kermit { + export { model, signed, crc17kermit as unsigned }; + export type { CRCBufferSource }; +} + +export default crc17kermit; diff --git a/types/crc/crc17modbus.d.ts b/types/crc/crc17modbus.d.ts new file mode 100644 index 0000000000..815ef2a702 --- /dev/null +++ b/types/crc/crc17modbus.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc-16-modbus'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc17modbus(buf: CRCBufferSource, previous?: number): number; +declare namespace crc17modbus { + export { model, signed, crc17modbus as unsigned }; + export type { CRCBufferSource }; +} + +export default crc17modbus; diff --git a/types/crc/crc17xmodem.d.ts b/types/crc/crc17xmodem.d.ts new file mode 100644 index 0000000000..fbe8064051 --- /dev/null +++ b/types/crc/crc17xmodem.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'xmodem'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc17xmodem(buf: CRCBufferSource, previous?: number): number; +declare namespace crc17xmodem { + export { model, signed, crc17xmodem as unsigned }; + export type { CRCBufferSource }; +} + +export default crc17xmodem; diff --git a/types/crc/crc24.d.ts b/types/crc/crc24.d.ts new file mode 100644 index 0000000000..c1a013443b --- /dev/null +++ b/types/crc/crc24.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc-24'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc24(buf: CRCBufferSource, previous?: number): number; +declare namespace crc24 { + export { model, signed, crc24 as unsigned }; + export type { CRCBufferSource }; +} + +export default crc24; diff --git a/types/crc/crc32.d.ts b/types/crc/crc32.d.ts new file mode 100644 index 0000000000..583d61aea4 --- /dev/null +++ b/types/crc/crc32.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc-32'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc32(buf: CRCBufferSource, previous?: number): number; +declare namespace crc32 { + export { model, signed, crc32 as unsigned }; + export type { CRCBufferSource }; +} + +export default crc32; diff --git a/types/crc/crc8.d.ts b/types/crc/crc8.d.ts new file mode 100644 index 0000000000..15b2e6afb6 --- /dev/null +++ b/types/crc/crc8.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'crc-8'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc8(buf: CRCBufferSource, previous?: number): number; +declare namespace crc8 { + export { model, signed, crc8 as unsigned }; + export type { CRCBufferSource }; +} + +export default crc8; diff --git a/types/crc/crc81wire.d.ts b/types/crc/crc81wire.d.ts new file mode 100644 index 0000000000..78bbbbb0a2 --- /dev/null +++ b/types/crc/crc81wire.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'dallas-1-wire'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc81wire(buf: CRCBufferSource, previous?: number): number; +declare namespace crc81wire { + export { model, signed, crc81wire as unsigned }; + export type { CRCBufferSource }; +} + +export default crc81wire; diff --git a/types/crc/crc91wire.d.ts b/types/crc/crc91wire.d.ts new file mode 100644 index 0000000000..7cd0d41f43 --- /dev/null +++ b/types/crc/crc91wire.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'dallas-1-wire'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crc91wire(buf: CRCBufferSource, previous?: number): number; +declare namespace crc91wire { + export { model, signed, crc91wire as unsigned }; + export type { CRCBufferSource }; +} + +export default crc91wire; diff --git a/types/crc/crcjam.d.ts b/types/crc/crcjam.d.ts new file mode 100644 index 0000000000..893a41f1cd --- /dev/null +++ b/types/crc/crcjam.d.ts @@ -0,0 +1,13 @@ +import type { CRCBufferSource } from '.'; +export type { CRCBufferSource }; + +export const model: 'jam'; +export function signed(buf: CRCBufferSource, previous?: number): number; + +declare function crcjam(buf: CRCBufferSource, previous?: number): number; +declare namespace crcjam { + export { model, signed, crcjam as unsigned }; + export type { CRCBufferSource }; +} + +export default crcjam; diff --git a/types/crc/index.d.ts b/types/crc/index.d.ts index 02db0e40b6..fbc94ad55a 100644 --- a/types/crc/index.d.ts +++ b/types/crc/index.d.ts @@ -1,17 +1,58 @@ -// Type definitions for crc 3.4 +// Type definitions for crc 3.8 // Project: https://github.com/alexgorbatchev/node-crc/ // Definitions by: Jianrong Yu +// ExE Boss // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -export function crc1(buf: string | Buffer, previous?: number): number; -export function crc8(buf: string | Buffer, previous?: number): number; -export function crc81wire(buf: string | Buffer, previous?: number): number; -export function crc16(buf: string | Buffer, previous?: number): number; -export function crc16ccitt(buf: string | Buffer, previous?: number): number; -export function crc16modbus(buf: string | Buffer, previous?: number): number; -export function crc16xmodem(buf: string | Buffer, previous?: number): number; -export function crc16kermit(buf: string | Buffer, previous?: number): number; -export function crc24(buf: string | Buffer, previous?: number): number; -export function crc32(buf: string | Buffer, previous?: number): number; +export type CRCBufferSource = + | string // force newline + | ArrayBufferLike + | ArrayLike + | Buffer + | NodeJS.TypedArray; + +import crc1 from './crc1'; +import crc8 from './crc8'; +import crc81wire from './crc81wire'; +import crc16 from './crc16'; +import crc16ccitt from './crc16ccitt'; +import crc16modbus from './crc16modbus'; +import crc16xmodem from './crc16xmodem'; +import crc16kermit from './crc16kermit'; +import crc24 from './crc24'; +import crc32 from './crc32'; +import crcjam from './crcjam'; + +export { + crc1, // force newline + crc8, + crc81wire, + crc16, + crc16ccitt, + crc16modbus, + crc16xmodem, + crc16kermit, + crc24, + crc32, + crcjam, +}; + +export default crc; +declare namespace crc { + export type { CRCBufferSource }; + export { + crc1, // force newline + crc8, + crc81wire, + crc16, + crc16ccitt, + crc16modbus, + crc16xmodem, + crc16kermit, + crc24, + crc32, + crcjam, + }; +} diff --git a/types/crc/lib/crc1.d.ts b/types/crc/lib/crc1.d.ts new file mode 100644 index 0000000000..d8ae67a74c --- /dev/null +++ b/types/crc/lib/crc1.d.ts @@ -0,0 +1,2 @@ +import crc1 from '../crc1'; +export = crc1; diff --git a/types/crc/lib/crc16.d.ts b/types/crc/lib/crc16.d.ts new file mode 100644 index 0000000000..eef14fb64a --- /dev/null +++ b/types/crc/lib/crc16.d.ts @@ -0,0 +1,2 @@ +import crc16 from '../crc16'; +export = crc16; diff --git a/types/crc/lib/crc16_ccitt.d.ts b/types/crc/lib/crc16_ccitt.d.ts new file mode 100644 index 0000000000..617d71314f --- /dev/null +++ b/types/crc/lib/crc16_ccitt.d.ts @@ -0,0 +1,2 @@ +import crc16_ccitt from '../crc16ccitt'; +export = crc16_ccitt; diff --git a/types/crc/lib/crc16_kermit.d.ts b/types/crc/lib/crc16_kermit.d.ts new file mode 100644 index 0000000000..3bb8fea0d6 --- /dev/null +++ b/types/crc/lib/crc16_kermit.d.ts @@ -0,0 +1,2 @@ +import crc16_kermit from '../crc16kermit'; +export = crc16_kermit; diff --git a/types/crc/lib/crc16_modbus.d.ts b/types/crc/lib/crc16_modbus.d.ts new file mode 100644 index 0000000000..55ab4bd1cb --- /dev/null +++ b/types/crc/lib/crc16_modbus.d.ts @@ -0,0 +1,2 @@ +import crc16_modbus from '../crc16modbus'; +export = crc16_modbus; diff --git a/types/crc/lib/crc16_xmodem.d.ts b/types/crc/lib/crc16_xmodem.d.ts new file mode 100644 index 0000000000..68ec425634 --- /dev/null +++ b/types/crc/lib/crc16_xmodem.d.ts @@ -0,0 +1,2 @@ +import crc16_xmodem from '../crc16xmodem'; +export = crc16_xmodem; diff --git a/types/crc/lib/crc24.d.ts b/types/crc/lib/crc24.d.ts new file mode 100644 index 0000000000..5564a003ea --- /dev/null +++ b/types/crc/lib/crc24.d.ts @@ -0,0 +1,2 @@ +import crc24 from '../crc24'; +export = crc24; diff --git a/types/crc/lib/crc32.d.ts b/types/crc/lib/crc32.d.ts new file mode 100644 index 0000000000..0b51c04a81 --- /dev/null +++ b/types/crc/lib/crc32.d.ts @@ -0,0 +1,2 @@ +import crc32 from '../crc32'; +export = crc32; diff --git a/types/crc/lib/crc8.d.ts b/types/crc/lib/crc8.d.ts new file mode 100644 index 0000000000..1d6e7f0eae --- /dev/null +++ b/types/crc/lib/crc8.d.ts @@ -0,0 +1,2 @@ +import crc8 from '../crc8'; +export = crc8; diff --git a/types/crc/lib/crc8_1wire.d.ts b/types/crc/lib/crc8_1wire.d.ts new file mode 100644 index 0000000000..e448d2a9b9 --- /dev/null +++ b/types/crc/lib/crc8_1wire.d.ts @@ -0,0 +1,2 @@ +import crc8_1wire from '../crc81wire'; +export = crc8_1wire; diff --git a/types/crc/lib/crcjam.d.ts b/types/crc/lib/crcjam.d.ts new file mode 100644 index 0000000000..20aaf07870 --- /dev/null +++ b/types/crc/lib/crcjam.d.ts @@ -0,0 +1,2 @@ +import crcjam from '../crcjam'; +export = crcjam; diff --git a/types/crc/lib/index.d.ts b/types/crc/lib/index.d.ts new file mode 100644 index 0000000000..ccf5761786 --- /dev/null +++ b/types/crc/lib/index.d.ts @@ -0,0 +1 @@ +export * from '..'; diff --git a/types/crc/test/lib.test.ts b/types/crc/test/lib.test.ts new file mode 100644 index 0000000000..58c95a3a1e --- /dev/null +++ b/types/crc/test/lib.test.ts @@ -0,0 +1,40 @@ +import * as fs from 'fs'; +import * as crc from 'crc/lib'; + +crc.default; // $ExpectError + +/** + * The `expectType` function from https://www.npmjs.com/package/tsd, + * except instead of returning `void`, it returns `T`. + */ +declare function expectType(t: T): T; + +// tests move from the readme of the module + +crc.crc32('hello').toString(16); +// "3610a686" + +crc.crc32(fs.readFileSync('README.md', 'utf8')).toString(16); +// "127ad531" +crc.crc32(fs.readFileSync('README.md')).toString(16); +// "127ad531" + +let value = crc.crc32('one'); +value = crc.crc32('two', value); +value = crc.crc32('three', value); +value.toString(16); + +// `crc.*`: +expectType(expectType(crc.crc1)); +expectType(expectType(crc.crc8)); +expectType(expectType(crc.crc81wire)); + +expectType(expectType(crc.crc16)); +expectType(expectType(crc.crc16ccitt)); +expectType(expectType(crc.crc16kermit)); +expectType(expectType(crc.crc16modbus)); +expectType(expectType(crc.crc16xmodem)); + +expectType(expectType(crc.crc24)); +expectType(expectType(crc.crc32)); +expectType(expectType(crc.crcjam)); diff --git a/types/crc/tsconfig.json b/types/crc/tsconfig.json index b1e471522a..44317644b1 100644 --- a/types/crc/tsconfig.json +++ b/types/crc/tsconfig.json @@ -17,7 +17,8 @@ "forceConsistentCasingInFileNames": true }, "files": [ - "index.d.ts", - "crc-tests.ts" + "crc-tests.ts", + "test/lib.test.ts", + "index.d.ts" ] } diff --git a/types/crc/tslint.json b/types/crc/tslint.json index 794cb4bf3e..a6a875f870 100644 --- a/types/crc/tslint.json +++ b/types/crc/tslint.json @@ -1 +1,11 @@ -{ "extends": "@definitelytyped/dtslint/dt.json" } +{ + "extends": "@definitelytyped/dtslint/dt.json", + "rules": { + "npm-naming": [true, { + "mode": "code", + "errors": [ + ["NoDefaultExport", false] + ] + }] + } +}