From b9f8703948804dc4495efd76f56ef35ba586ec12 Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:28:18 +0200 Subject: [PATCH 1/7] Add toArray function --- src/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/index.js b/src/index.js index 65ffb87..720f1f6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,17 @@ import { astralRange } from './string'; +/** + * Converts a string to an array of string chars + * @param {string} str The string to turn into array + * @returns {string[]} + */ +export function toArray(str) { + if (typeof str !== 'string') { + throw new Error('A string is expected as input'); + } + return str.match(astralRange) || []; +} + /** * Returns the length of a string * From 357459b9fd06a87bbf7b89b09e9b918d5e1ffc4a Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:28:25 +0200 Subject: [PATCH 2/7] Add toArray tests --- test/toArray.test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/toArray.test.js diff --git a/test/toArray.test.js b/test/toArray.test.js new file mode 100644 index 0000000..623ad9a --- /dev/null +++ b/test/toArray.test.js @@ -0,0 +1,42 @@ +import assert from 'assert'; +import { toArray } from '../src/index'; + +describe('Convert String to Array', () => { + it('Converts a simple string to array', () => { + assert.deepEqual(toArray('abcdefg'), ['a', 'b', 'c', 'd', 'e', 'f', 'g']); + }); + it('Converts unicode strings to array', () => { + assert.deepEqual(toArray('๐Ÿ”ฅ๐Ÿ‘๐Ÿฝ'), ['๐Ÿ”ฅ', '๐Ÿ‘๐Ÿฝ']); + assert.deepEqual(toArray('Iรฑtรซrnรขtiรดnร lizรฆtiรธnโ˜ƒ'), [ + 'I', + 'รฑ', + 't', + 'รซ', + 'r', + 'n', + 'รข', + 't', + 'i', + 'รด', + 'n', + 'ร ', + 'l', + 'i', + 'z', + 'รฆ', + 't', + 'i', + 'รธ', + 'n', + 'โ˜ƒ' + ]); + }); + it('Throws an error for non-strings', () => { + assert.throws(() => toArray({}), Error); + assert.throws(() => toArray(1), Error); + assert.throws(() => toArray(null), Error); + }); + it('Converts empty string to empty array', () => { + assert.deepEqual(toArray(''), []); + }); +}); From 6fd8ff92738e614cf8e3211ceac987542e2e8a12 Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:29:21 +0200 Subject: [PATCH 3/7] Add typings --- index.d.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index e00ac91..5a70019 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,9 +1,19 @@ declare namespace stringz { + export function toArray(str: string): string[]; export function length(str: string): number; export function substring(str: string, begin?: number, end?: number): string; export function substr(str: string, begin?: number, length?: number): string; - export function limit(str: string, limit?: number, padString?: string, padPosition?: 'right'|'left') : string; - export function indexOf(str: string, searchStr: string, position?: number): number; + export function limit( + str: string, + limit?: number, + padString?: string, + padPosition?: 'right' | 'left' + ): string; + export function indexOf( + str: string, + searchStr: string, + position?: number + ): number; } export = stringz; From 5ddb045a38a4114355754de8acb840739ab89e61 Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:31:45 +0200 Subject: [PATCH 4/7] Update README --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 733c529..9438153 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ const { limit, substr } = require('stringz'); * [`substring()`](#substring) * [`substr()`](#substr) * [`indexOf()`](#indexof) +* [`toArray()`](#toarray) ### Limit String to Width @@ -137,6 +138,20 @@ indexOf('Emojis ๐Ÿ‘๐Ÿฝ are ๐Ÿ† poison. ๐ŸŒฎs are bad.', 'are'); // 9 indexOf('Emojis ๐Ÿ‘๐Ÿฝ are ๐Ÿ† poison. ๐ŸŒฎs are bad.', 'are', 10); // 26 ``` +### ToArray + + function toArray(str) + +| Param | Type | Default | Description | +| ----- | ------------------- | ------- | -------------------------- | +| str | String | _none_ | String to convert to array | + +#### Examples + +```javascript +indexOf('๐Ÿ‘๐Ÿฝ๐Ÿ†๐ŸŒฎ'); // ['๐Ÿ‘๐Ÿฝ', '๐Ÿ†', '๐ŸŒฎ'] +``` + ## Test ```bash From 04dd2be55fa10cfdd0acf5edeb7ef1e6feba4716 Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:32:02 +0200 Subject: [PATCH 5/7] 0.4.0 --- package-lock.json | 2 +- package.json | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7448969..a297234 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "stringz", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a782367..31a2e74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stringz", - "version": "0.3.0", + "version": "0.4.0", "description": "Zero-dependency unicode-aware string tools", "main": "dist/index.js", "types": "./index.d.ts", @@ -13,7 +13,10 @@ "prepublish": "babel src --out-dir dist" }, "lint-staged": { - "{src,test,benchmark}/**/*.js": ["prettier --write", "git add"] + "{src,test,benchmark}/**/*.js": [ + "prettier --write", + "git add" + ] }, "repository": { "type": "git", From 17f1cc8d81f71439d6c63f7c267f45f83b51a03d Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:32:50 +0200 Subject: [PATCH 6/7] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7a5887..3b08dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ | Version | Date | Notes | | ------- | ---------- | ------------------------------------------------------------------- | +| 0.4.0 | 2017-11-29 | [New `toArray` function](https://github.com/sallar/stringz/pull/24) | | 0.3.0 | 2017-11-29 | [New `indexOf` function](https://github.com/sallar/stringz/pull/22) | | 0.2.3 | 2017-09-19 | Add `.babelrc` to `.gitignore` | | 0.2.2 | 2017-06-20 | Fix Typescript Definition Issue #14 | From 03a13aecfbf1024dcd1530424cf09ff461b67bf5 Mon Sep 17 00:00:00 2001 From: Sallar Kaboli Date: Fri, 8 Dec 2017 19:37:10 +0200 Subject: [PATCH 7/7] Update date on changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b08dd3..f2a55c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ | Version | Date | Notes | | ------- | ---------- | ------------------------------------------------------------------- | -| 0.4.0 | 2017-11-29 | [New `toArray` function](https://github.com/sallar/stringz/pull/24) | +| 0.4.0 | 2017-12-08 | [New `toArray` function](https://github.com/sallar/stringz/pull/24) | | 0.3.0 | 2017-11-29 | [New `indexOf` function](https://github.com/sallar/stringz/pull/22) | | 0.2.3 | 2017-09-19 | Add `.babelrc` to `.gitignore` | | 0.2.2 | 2017-06-20 | Fix Typescript Definition Issue #14 |