# @qfin/tools

> The awesome repo for Quantofin tools

Latest version **0.7.0** (published 2018-11-28) · Proprietary license · 0 weekly downloads

## Install

```sh
npm install @qfin/tools
pnpm add @qfin/tools
yarn add @qfin/tools
bun add @qfin/tools
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2018-11-28 |
| First published | 2018-11-17 |
| Weekly downloads | 0 |
| License | Proprietary |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 72.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sohail Alam |
| Maintainers | sohailalam2 |

## Links

- npm: https://www.npmjs.com/package/@qfin/tools
- npm.io page: https://npm.io/package/@qfin/tools

## Recent versions

- 0.7.0 (latest) — 2018-11-28
- 0.6.0 — 2018-11-27
- 0.5.0 — 2018-11-27
- 0.2.0 — 2018-11-17

## README

# @qfin/tools

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./generate-docs.js in the project root. -->

---

This module is part of the [Quantofin project](https://quantofin.com). 

> The awesome @qfin/tools for Quantofin apps ❤

## 💾 Install

```bash
npm install --save @qfin/tools
```

## ⚙ Module documentation


<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

## base64.encode

Encode a string to base64

Type: module.base64.encode

### Parameters

-   `value` **[string][1]** The string that needs to encoded to base64 encoding

### Examples

```javascript
const { base64 } = require('@qfin/tools');

const encoded = base64.encode('Hello World');
// 'SGVsbG8gV29ybGQ='
```

Returns **[string][1]** the encoded value

## base64.encodeObject

Encode a object to base64 string by first serializing
the object through JSON.stringify

Type: module.base64.encodeObject

### Parameters

-   `objectToEncode` **[object][2]** The object to encode

### Examples

```javascript
const { base64 } = require('@qfin/tools');

const encoded = base64.encodeObject({ message: 'Hello World' });
// 'eyJtZXNzYWdlIjoiSGVsbG8gV29ybGQifQ=='
```

Returns **[string][1]** the encoded value

## base64.decode

Decode a base64 encoded string to string data

Type: module.base64.decode

### Parameters

-   `encodedData` **[string][1]** base64 encoded string that needs to be decoded

### Examples

```javascript
const { base64 } = require('@qfin/tools');

const decoded = base64.decode('SGVsbG8gV29ybGQ=');
// 'Hello World'
```

Returns **[string][1]** the decoded value

## base64.decodeAsObject

Decode a base64 encoded string to javascript object
by parsing the decoded string through JSON.parse

Type: module.base64.decodeAsObject

### Parameters

-   `encodedData` **[string][1]** the encoded base64 string

### Examples

```javascript
const decoded = base64.decodeAsObject('eyJtZXNzYWdlIjoiSGVsbG8gV29ybGQifQ==');
// { message: 'Hello World' }
```

Returns **[object][2]** the decoded value

## BufferSplitter

The BufferSplitter class

This class can help transform data stream with a given delimiter into chunks upto the delimiter

Type: module.BufferSplitter

### Examples

```javascript
const { BufferSplitter } = require('@qfin/tools');

const NewLineBufferSplitter = new BufferSplitter('\n');

function transformer(lines) {
   return csvToJsonTransformer(lines, headers);
}

// .... when new data comes in
const csvArrays = NewLineBufferSplitter.transform(buffer, transformer);

// ... when stream is flushed
const csvArrays = NewLineBufferSplitter.finishTransform(transformer);
```

### split

Split a given buffer by searching for the deliminating character

#### Parameters

-   `buffer` **[Buffer][3]** The buffer that has to be split

Returns **[Buffer][3]** The splitted buffer that can be safely read and parsed as required

### finishSplit

Finish splitting the buffer and get the remaining chunk of data

Returns **[Buffer][3]** The remaining chunk of data that does not have the deliminating character

### transform

Transform a buffer using a transforming function

If a transforming function is not present that it is assumed that the buffer contains comma separated values (CSV)
deliminated by the delimiter (defaults to new line) and then will be presented as an array of csv data

#### Parameters

-   `buffer` **[Buffer][3]** The buffer that needs to be transformed
-   `transformer` **[function][4]?** The transforming function, should take a string parameter

Returns **any** 

### finishTransform

Finish the transform by calling this method.
This will run the transforming function through the remaining buffer

#### Parameters

-   `transformer` **[function][4]?** The transforming function, should take a string parameter

Returns **any** 

## BufferSplitter.NewLineBufferSplitter

A BufferSplitter with deliminator as new line `\n`

Type: module.BufferSplitter.NewLineBufferSplitter

Returns **[BufferSplitter][5]** 

## dotObjectPath.getValue

Gets the object's value by key with dot path

Type: module.dotObjectPath.getValue

### Parameters

-   `obj` **[object][2]** The data object
-   `keyPath` **[string][1]** The dot separated key path

### Examples

```javascript
const { dotObjectPath } = require('@qfin/tools');

const obj = { a: { b: { c: 100 } } };

const val = dotObjectPath.getValue(obj, 'a.b');
// val = { c: 100 }

const val = dotObjectPath.getValue(obj, 'a.b.c');
// val = 100
```

Returns **any** The value at the path

## dotObjectPath.setValue

Sets the value of the object at the given dot path

Type: module.dotObjectPath.setValue

### Parameters

-   `obj` **[object][2]** The data object
-   `keyPath` **[string][1]** The dot separated key path
-   `value` **any** The value to be set at the path

### Examples

```javascript
const { dotObjectPath } = require('@qfin/tools');

const obj = { a: {} };

dotObjectPath.setValue(obj, 'a.b.c', 100);
// obj = { a: { b: { c: 100 } } };
```

## filesystem.readFileAsStream

Creates a readable stream from a file

Type: module.filesystem.readFileAsStream

### Parameters

-   `filepath` **[string][1]** The filepath without the filename
-   `filename` **[string][1]** The filename
-   `options` **[object][2]** NodeJS fs options (encoding defaults to utf-8)

Returns **ReadableStream** ReadableStream

## filesystem.writeFileAsStream

Creates a writable stream to stream data to a file

Type: module.filesystem.writeFileAsStream

### Parameters

-   `filepath` **[string][1]** The filepath without the filename
-   `filename` **[string][1]** The filename
-   `options` **[object][2]** NodeJS fs options (encoding defaults to utf-8)

Returns **WritableStream** WritableStream

## filesystem.writeToFile

Writes to a file by streaming the data by creating a writable stream internally

Type: module.filesystem.writeToFile

### Parameters

-   `data` **([Buffer][3] \| [string][1])** The data to write to the file
-   `filepath` **[string][1]** The filepath without the filename
-   `filename` **[string][1]** The filename
-   `options` **[object][2]** NodeJS fs options (encoding defaults to utf-8)

Returns **[Promise][6]&lt;any>** resolves to nothing or rejects with error

## filesystem.streamToFile

Stream data to a file (useful for piping a stream)

Type: module.filesystem.streamToFile

### Parameters

-   `filepath` **[string][1]** The filepath without the filename
-   `filename` **[string][1]** The filename

Returns **Transform** Transform

## murmurhash2

This hash function takes in a string and hashes it to a unique number value

murmurhash2 algorithm adapted from [https://gist.github.com/raycmorgan/588423][7]

Type: module.murmurhash2

### Parameters

-   `str` **[string][1]** The string to hash
-   `seed` **[string][1]?** The seed value for this hash, defaults to the length of the string

Returns **[number][8]** The hashed value of the string

## transform.csvToJsonTransformer

Transforms CSV lines to its json counterpart

Type: module.transform.csvToJsonTransformer

### Parameters

-   `lines` **[array][9]** The array of lines containing the csv data (required)
-   `headers` **[array][9]** The headers which become the keys of json (required)
    The header values can be dot.separated i.e. 'profile.name' will become { profile: { name: 'value' } }
    Also, a special header value of underscore (\_) can be used to skip the header and value assignment
-   `delimiter` **[string][1]?** The line item delimiter (defaults to comma)
-   `trim` **[boolean][10]?** Whether to trim the values or not (defaults to true)

Returns **[array][9]** Array(Object) JSON Objects

## transform.csvToJsonStreamTransformer

Transform a CSV buffer Stream to a JSON stream

Type: module.transform.csvToJsonStreamTransformer

### Parameters

-   `headers` **[array][9]** The headers which become the keys of json (required)
    The header values can be dot.separated i.e. 'profile.name' will become { profile: { name: 'value' } }
    Also, a special header value of underscore (\_) can be used to skip the header and value assignment
-   `callback` **[function][4]** The callback which receives the error or
    final array of json objects when the stream terminates (required)

### Examples

```javascript
const { transformers } = require('@qfin/tools');

res.data
   .pipe(() =>
     transformers.csvToJsonStreamTransformer(headers, async (err, json) => {
       if (err) {
         throw err;
       } else {
         await processData(json);
       }
     })
   )
   .on('close', () => console.log('Stream closed'))
   .on('error', (err) => console.error(err));
```

Returns **Transform** Transform

[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[3]: https://nodejs.org/api/buffer.html

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function

[5]: #buffersplitter

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise

[7]: https://gist.github.com/raycmorgan/588423

[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

---
_Source: https://npm.io/package/@qfin/tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
