Skip to content

Commit

Permalink
Add string.match
Browse files Browse the repository at this point in the history
  • Loading branch information
YoranBrondsema authored and Kelly Selden committed May 5, 2018
1 parent e9328c0 commit baf0796
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ import { nameOfMacro } from 'ember-awesome-macros';
* [`string.isHtmlSafe`](#stringishtmlsafe)
* [`string.lastIndexOf`](#stringlastindexof)
* [`string.length`](#stringlength)
* [`string.match`](#stringmatch)
* [`string.replace`](#stringreplace)
* [`string.split`](#stringsplit)
* [`string.substr`](#stringsubstr)
Expand Down Expand Up @@ -1021,6 +1022,19 @@ example: string.length('string1'), // 3
composingExample: string.length(tag`${'string1'}${'string2'}`) // 6
```

##### `string.match`
wraps [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match), allows composing

```js
string1: 'abc',
string2: 'xyz',
regex1: /abc/,
regex2: /xyz/,
example: string.match('string1', 'regex1'), // ['abc']
example: string.match('string1', 'regex2'), // null
composingExample: string.match(tag`${'string1'}${'string2'}`, 'regex2') // ['xyz']
```

##### `string.replace`
wraps [`String.prototype.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), allows composing

Expand Down
1 change: 1 addition & 0 deletions addon/string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { default as indexOf } from './index-of';
export { default as isHtmlSafe } from './is-html-safe';
export { default as lastIndexOf } from './last-index-of';
export { default as length } from './length';
export { default as match } from './match';
export { default as replace } from './replace';
export { default as split } from './split';
export { default as substr } from './substr';
Expand Down
3 changes: 3 additions & 0 deletions addon/string/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { normalizeString2 } from './-utils';

export default normalizeString2('match');
5 changes: 5 additions & 0 deletions tests/acceptance/string-imports-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import string, {
isHtmlSafe,
lastIndexOf,
length,
match,
replace,
split,
substr,
Expand All @@ -33,6 +34,7 @@ import _indexOf from 'ember-awesome-macros/string/index-of';
import _isHtmlSafe from 'ember-awesome-macros/string/is-html-safe';
import _lastIndexOf from 'ember-awesome-macros/string/last-index-of';
import _length from 'ember-awesome-macros/string/length';
import _match from 'ember-awesome-macros/string/match';
import _replace from 'ember-awesome-macros/string/replace';
import _split from 'ember-awesome-macros/string/split';
import _substr from 'ember-awesome-macros/string/substr';
Expand All @@ -59,6 +61,7 @@ test('all string global imports', function(assert) {
assert.ok(string.isHtmlSafe);
assert.ok(string.lastIndexOf);
assert.ok(string.length);
assert.ok(string.match);
assert.ok(string.replace);
assert.ok(string.split);
assert.ok(string.substr);
Expand All @@ -84,6 +87,7 @@ test('all string imports', function(assert) {
assert.ok(isHtmlSafe);
assert.ok(lastIndexOf);
assert.ok(length);
assert.ok(match);
assert.ok(replace);
assert.ok(split);
assert.ok(substr);
Expand All @@ -109,6 +113,7 @@ test('all string default imports', function(assert) {
assert.ok(_isHtmlSafe);
assert.ok(_lastIndexOf);
assert.ok(_length);
assert.ok(_match);
assert.ok(_replace);
assert.ok(_split);
assert.ok(_substr);
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/string/match-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { match } from 'ember-awesome-macros/string';
import { raw } from 'ember-awesome-macros';
import { computed } from '@ember/object';
import { module, test } from 'qunit';
import compute from 'ember-macro-test-helpers/compute';
import sinon from 'sinon';

module('Integration | Macro | string | match');

test('it calls match on string', function(assert) {
compute({
assert,
computed: match('string', 'regex'),
properties: {
string: 'abcxyz',
regex: /abc/
},
deepEqual: ['abc']
});
});

test('doesn\'t calculate when unnecessary', function(assert) {
let callback = sinon.spy();

compute({
computed: match(
undefined,
computed(callback)
)
});

assert.notOk(callback.called);
});

test('composable: it calls match on string', function(assert) {
compute({
assert,
computed: match(
raw('abcxyz'),
raw(/abc/)
),
deepEqual: ['abc']
});
});

0 comments on commit baf0796

Please sign in to comment.