Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To array #24

Merged
merged 7 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

| Version | Date | Notes |
| ------- | ---------- | ------------------------------------------------------------------- |
| 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 |
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const { limit, substr } = require('stringz');
* [`substring()`](#substring)
* [`substr()`](#substr)
* [`indexOf()`](#indexof)
* [`toArray()`](#toarray)

### Limit String to Width

Expand Down Expand Up @@ -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 | <code>String</code> | _none_ | String to convert to array |

#### Examples

```javascript
indexOf('👍🏽🍆🌮'); // ['👍🏽', '🍆', '🌮']
```

## Test

```bash
Expand Down
14 changes: 12 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down
42 changes: 42 additions & 0 deletions test/toArray.test.js
Original file line number Diff line number Diff line change
@@ -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(''), []);
});
});