columnify 1.5: Add type definitions (#39415)

* Generate files for columnify.

* Add decl and tests.

* Lint fixes.
This commit is contained in:
garyking 2019-10-25 16:27:47 -04:00 committed by Wesley Wigham
parent a68b429d53
commit 74240e3686
4 changed files with 258 additions and 0 deletions

View File

@ -0,0 +1,198 @@
import columnify = require('columnify');
/**
* Objects
*/
let data: any = {
'commander@0.6.1': 1,
'minimatch@0.2.14': 3,
'mkdirp@0.3.5': 2,
'sigmund@1.0.0': 3,
};
columnify(data);
/**
* Column Names
*/
data = {
'commander@0.6.1': 1,
'minimatch@0.2.14': 3,
'mkdirp@0.3.5': 2,
'sigmund@1.0.0': 3,
};
columnify(data, { columns: ['MODULE', 'COUNT'] });
/**
* Arrays of Objects
*/
columnify([
{
name: 'mod1',
version: '0.0.1',
},
{
name: 'module2',
version: '0.2.0',
},
]);
/**
* Filtering & Ordering
*/
data = [
{
name: 'module1',
description: 'some description',
version: '0.0.1',
},
{
name: 'module2',
description: 'another description',
version: '0.2.0',
},
];
columnify(data, {
columns: ['name', 'version'],
});
/**
* Maximum and Minimum Column Widths
*/
columnify(
[
{
name: 'mod1',
description: 'some description which happens to be far larger than the max',
version: '0.0.1',
},
{
name: 'module-two',
description: 'another description larger than the max',
version: '0.2.0',
},
],
{
minWidth: 20,
config: {
description: { maxWidth: 30 },
},
},
);
/**
* Truncating
*/
columnify(data, {
truncate: true,
config: {
description: {
maxWidth: 20,
},
},
});
/**
* Align Right/Center
*/
data = {
'mocha@1.18.2': 1,
'commander@2.0.0': 1,
'debug@0.8.1': 1,
};
columnify(data, { config: { value: { align: 'right' } } });
/**
* Padding Character
*/
data = {
shortKey: 'veryVeryVeryVeryVeryLongVal',
veryVeryVeryVeryVeryLongKey: 'shortVal',
};
columnify(data, { paddingChr: '.' });
/**
* Preserve Existing Newlines
*/
data = [
{
name: 'glob@3.2.9',
paths: ['node_modules/tap/node_modules/glob', 'node_modules/tape/node_modules/glob'].join('\n'),
},
{
name: 'nopt@2.2.1',
paths: ['node_modules/tap/node_modules/nopt'],
},
{
name: 'runforcover@0.0.2',
paths: 'node_modules/tap/node_modules/runforcover',
},
];
columnify(data, { preserveNewLines: true });
/**
* Custom Truncation Marker
*/
columnify(data, {
truncate: true,
truncateMarker: '>',
widths: {
description: {
maxWidth: 20,
},
},
});
/**
* Custom Column Splitter
*/
columnify(data, {
columnSplitter: ' | ',
});
/**
* Control Header Display
*/
columnify(data, {
showHeaders: false,
});
columnify(data, {
config: {
id: { showHeaders: false },
},
});
/**
* Transforming Column Data and Headers
*/
columnify(
[
{
name: 'mod1',
description: 'SOME DESCRIPTION TEXT.',
},
{
name: 'module-two',
description: 'SOME SLIGHTLY LONGER DESCRIPTION TEXT.',
},
],
{
dataTransform(data) {
return data.toLowerCase();
},
config: {
name: {
headingTransform(heading) {
heading = 'module ' + heading;
return `*${heading.toUpperCase()}*`;
},
},
},
},
);

36
types/columnify/index.d.ts vendored Normal file
View File

@ -0,0 +1,36 @@
// Type definitions for columnify 1.5
// Project: https://github.com/timoxley/columnify
// Definitions by: Gary King <https://github.com/garyking>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
declare function columnify(data: Record<string, any> | any[], options?: columnify.GlobalOptions): string;
declare namespace columnify {
interface Options {
align?: 'left' | 'center' | 'centre' | 'right';
dataTransform?: (data: string) => string;
headingTransform?: (data: string) => string;
minWidth?: number;
maxWidth?: number;
paddingChr?: string;
preserveNewLines?: boolean;
showHeaders?: boolean;
truncateMarker?: string;
}
interface GlobalOptions extends Options {
columns?: string[];
columnSplitter?: string;
config?: {
[columnName: string]: Options;
};
maxLineWidth?: number;
truncate?: boolean;
widths?: {
[columnName: string]: Pick<Options, 'minWidth' | 'maxWidth'>;
};
}
}
export = columnify;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"columnify-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }