diff --git a/README.md b/README.md index 373c50f..8e1b0b0 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/addon/string/index.js b/addon/string/index.js index 0caee50..b221ca5 100644 --- a/addon/string/index.js +++ b/addon/string/index.js @@ -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'; diff --git a/addon/string/match.js b/addon/string/match.js new file mode 100644 index 0000000..6f72e0e --- /dev/null +++ b/addon/string/match.js @@ -0,0 +1,3 @@ +import { normalizeString2 } from './-utils'; + +export default normalizeString2('match'); diff --git a/tests/acceptance/string-imports-test.js b/tests/acceptance/string-imports-test.js index 9f2b845..f439164 100644 --- a/tests/acceptance/string-imports-test.js +++ b/tests/acceptance/string-imports-test.js @@ -12,6 +12,7 @@ import string, { isHtmlSafe, lastIndexOf, length, + match, replace, split, substr, @@ -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'; @@ -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); @@ -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); @@ -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); diff --git a/tests/integration/string/match-test.js b/tests/integration/string/match-test.js new file mode 100644 index 0000000..8ce796b --- /dev/null +++ b/tests/integration/string/match-test.js @@ -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'] + }); +});