{"size": 2168, "ext": "js", "max_stars_repo_path": "packages/tools/utils/src/modules/string-array.js", "max_stars_repo_name": "nestauk/svizzle", "max_stars_repo_stars_event_min_datetime": "2020-07-31T08:51:21.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-14T17:28:57.000Z", "max_issues_repo_path": "packages/tools/utils/src/modules/string-array.js", "max_issues_repo_name": "nestauk/svizzle", "max_issues_repo_issues_event_min_datetime": "2019-02-21T10:54:28.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T17:36:03.000Z", "max_forks_repo_path": "packages/tools/utils/src/modules/string-array.js", "max_forks_repo_name": "nestauk/svizzle", "max_forks_repo_forks_event_min_datetime": "2020-01-18T12:16:17.000Z", "max_forks_repo_forks_event_max_datetime": "2020-01-18T12:16:17.000Z"}
{"max_stars_count": 9, "max_issues_count": 271, "max_forks_count": 1, "avg_line_length": 17.6260162602, "max_line_length": 83, "alphanum_fraction": 0.6111623616}
/**
* @module @svizzle/utils/string-array
*/

import * as _ from 'lamb';

import {reduceFromEmptyArray} from './[any-any]:accumcb-[array-any]';
import {trim} from './string_proto-string';
import {isTrimmedNotEmpty} from './string-boolean';
import {makeSplitBy} from './string-[string-array]';

/**
 * Return an array by splitting by '.'
 * @see {@link module:@svizzle/utils/string-[string-array].makeSplitBy|makeSplitBy}
 *
 * @function
 * @arg {string} string
 * @return {array}
 *
 * @example
> splitByDot('a.b.c')
['a', 'b', 'c']
 *
 * @since 0.1.0
 */
export const splitByDot = makeSplitBy('.');

/**
 * Return an array by splitting by '\n'
 * @see {@link module:@svizzle/utils/string-[string-array].makeSplitBy|makeSplitBy}
 *
 * @function
 * @arg {string} string
 * @return {array}
 *
 * @example
> splitByDot('a\nb\nc')
['a', 'b', 'c']
 *
 * @since 0.1.0
 */
export const splitByEOL = makeSplitBy('\n');

/**
 * Return an array by splitting by ';'
 * @see {@link module:@svizzle/utils/string-[string-array].makeSplitBy|makeSplitBy}
 *
 * @function
 * @arg {string} string
 * @return {array}
 *
 * @example
> splitBySemiColon('A;B;C')
['A', 'B', 'C']
 *
 * @since 0.1.0
 */
export const splitBySemiColon = makeSplitBy(';');

/**
 * Return rows in a string
 *
 * @function
 * @arg {string} string
 * @return {array}
 *
 * @example
> makeRows('A,B\n1,2\n3,4\n')
['A,B', '1,2', '3,4']
 *
 * @since 0.1.0
 */
export const makeLines = _.pipe([
	trim,
	splitByEOL
]);

/**
 * Return rows in a string excluding the first line (the header).
 * Useful for CSVs.
 *
 * @function
 * @arg {string} string
 * @return {array}
 *
 * @example
> makeRows('A,B\n1,2\n3,4\n')
['1,2', '3,4']
 *
 * @since 0.1.0
 */
export const makeRows = _.pipe([
	trim,
	splitByEOL,
	_.tail
]);

/* ndjson */

/**
 * Return an array from a ndjson string
 *
 * @function
 * @arg {string} ndjsonString
 * @return {array}
 *
 * @example
> ndjsonToArray('{'a':1}\n{'b':2}\n\n')
[{a: 1}, {b: 2}]
 *
 * @since 0.1.0
 */
export const ndjsonToArray = _.pipe([
	splitByEOL,
	reduceFromEmptyArray((array, str) => {
		if (isTrimmedNotEmpty(str)) {
			array.push(JSON.parse(str))
		}

		return array;
	})
]);
