Skip to content

Instantly share code, notes, and snippets.

@SaladFork
Last active February 2, 2017 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaladFork/178b2408d025d7c0d2acaddf22bbe8bb to your computer and use it in GitHub Desktop.
Save SaladFork/178b2408d025d7c0d2acaddf22bbe8bb to your computer and use it in GitHub Desktop.
ember-computed-template-string with RegExp

Copyright (c) 2016-2017 Elad Shahar and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import Ember from 'ember';
import computedTemplateString from '../utils/computed-template-string';
export default Ember.Controller.extend({
});
<ul>
{{greeting}}
{{nested}}
</ul>
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
import { moduleFor, test } from 'ember-qunit';
import computedTemplateString from 'twiddle/utils/computed-template-string';
// Copied from intercom/ember-computed-template-string tests
// See: <https://github.com/intercom/ember-computed-template-string/blob/dc90722/tests/unit/custom-replace-call-paths-test.js>
module('Computed template string with an import');
const Person = Em.Object.extend({
name: 'Alex',
age: 2,
config: {
path: '/home'
},
greeting: computedTemplateString('Hello ${name}, you are ${age} years old'),
nested: computedTemplateString('the path is ${config.path}, ok?'),
multi: computedTemplateString('hi ${name}, bye ${name}'),
withSingleQuote: computedTemplateString("Single quotes in literal: '' and in property: ${name}"),
withDoubleQuote: computedTemplateString('Double quotes in literal: "" and in property: ${name}'),
});
test('A template with two properties', function(assert) {
var person = Person.create({ name: 'Alex', age: 2 });
assert.equal(person.get('greeting'), 'Hello Alex, you are 2 years old');
person.setProperties({ name: 'Ben', age: 1 });
assert.equal(person.get('greeting'), 'Hello Ben, you are 1 years old');
});
test('A template with a nested property', function(assert) {
var person = Person.create();
assert.equal(person.get('nested'), 'the path is /home, ok?');
person.set('config.path', '/garden');
assert.equal(person.get('nested'), 'the path is /garden, ok?');
});
test('A template with a key used multiple times', function(assert) {
var person = Person.create({ name: 'Ben' });
assert.equal(person.get('multi'), 'hi Ben, bye Ben');
person.set('name', 'Sarah');
assert.equal(person.get('multi'), 'hi Sarah, bye Sarah');
});
test('A template with a single quote', function(assert) {
var person = Person.create({ name: 'Ben' });
assert.equal(person.get('withSingleQuote'), "Single quotes in literal: '' and in property: Ben");
person.set('name', "Mr O'Shea");
assert.equal(person.get('withSingleQuote'), "Single quotes in literal: '' and in property: Mr O'Shea");
});
test('A template with a double quote', function(assert) {
var person = Person.create({ name: 'Ben' });
assert.equal(person.get('withDoubleQuote'), 'Double quotes in literal: "" and in property: Ben');
person.set('name', 'Mr O"Shea');
assert.equal(person.get('withDoubleQuote'), 'Double quotes in literal: "" and in property: Mr O"Shea');
});
{
"version": "0.10.5",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
import Ember from 'ember';
// http://regexr.com/3e9a3
const propertyRegEx = /\$\{([^\b]+?)\}/g;
export default function computedTemplateString(string) {
const dependentKeys = getAllMatches(string, propertyRegEx).uniq();
return Ember.computed(...dependentKeys, function() {
return string.replace(propertyRegEx, (match, property) => {
return Ember.get(this, property);
});
});
}
// To get all capturing groups in a RegEx, we have to run `exec` repeatedly.
// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Finding_successive_matches>
function getAllMatches(string, regEx) {
const regExp = new RegExp(regEx);
const matches = Ember.A();
let matchArray;
while (matchArray = regExp.exec(string)) {
const [_matchingString, match] = matchArray;
matches.push(match);
}
return matches;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment