@metricinsights/cs-helper
v0.3.2
Published
Helper for Custom Scripts
Downloads
14
Readme
cs-helper
Metric Insights Custom Script helper
Install
npm i --save-dev @metricinsights/cs-helper
Usage
CLI usage
Add cs-helper
as package.json script to build you code into a bundle
{
"name": "package-name",
"version": "1.0.0",
"description": "Package description",
"scripts": {
"build": "@metricinsights/cs-helper <path-to-index.js>"
}
}
Run command to build your code npm run build
API options
| Option | Description | Default |
|-----------|------------------------------------|---------|
| --clean
| Clean dist folder before build | false
|
| --v7
| CS will be compatible only with v7 | false
|
Basic usage
import { cs, parseParams } from '@metricinsights/cs-helper';
const parsedParams = parseParams({ defaultValue: 1 }); // { defaultValue: 1 } & cs.params
setTimeout(() => {
cs.close();
}, 1000);
Utils usage
Data convertor
Contains methods to apply metadata for MI datasets
import { cs } from '@metricinsights/cs-helper';
import {
buildMetadataTransformer,
applyMetadata,
transformDataset,
} from '@metricinsights/cs-helper/dist/utils';
async function main() {
const dataset = await new Promise((resolve, reject) => {
cs.runApiRequest(
`/api/dataset_data?dataset=${1}`,
Object.assign({}, params, {
success: resolve,
error: reject,
}),
);
}); // { data: [{ key1: value1, key2: value2 }], metadata: [{ name: key1, type: 'numeric' }, { name: key2, type: 'text' }] }
const metadataTransformer = buildMetadataTransformer(dataset.metadata); // { [key1]: (v) => Number(v), [key2]: (v) => String(v) }
const transformedData = applyMetadata(metadataTransformer, dataset.data); // [{ key1: Number(value1), key2: String(value2) }]
const transformedResponse = transformedData(dataset); // { data: [{ key1: Number(value1), key2: String(value2) }], metadata: [{ name: key1, type: 'numeric' }, { name: key2, type: 'text' }] }
cs.log(JSON.stringify(metadataTransformer));
cs.log(JSON.stringify(transformedData));
cs.log(JSON.stringify(transformedResponse));
cs.close();
}
main();