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

Add String.prototype.match macro #445

Merged
merged 1 commit into from
May 5, 2018
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,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 @@ -987,6 +988,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']
});
});