Skip to content

Commit

Permalink
Auto merge of #87 - vikram7:vr-add-underscore, r=poteto
Browse files Browse the repository at this point in the history
Add string underscore helper

This PR adds the `underscore` helper per issue https://github.com/DockYard/ember-composable-helpers/issues/69
  • Loading branch information
homu committed Feb 26, 2016
2 parents 4020d4d + 6b39d8b commit d56d20c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,7 @@ For action helpers, this will mean better currying semantics:
* [String](#string-helpers)
+ [`capitalize`](#capitalize)
+ [`dasherize`](#dasherize)
+ [`underscore`](#underscore)
+ [`w`](#w)
* [Array](#array-helpers)
+ [`map-by`](#map-by)
Expand Down Expand Up @@ -146,6 +147,16 @@ Capitalizes a string using `Ember.String.dasherize`.
**[⬆️ back to top](#available-helpers)**
#### `underscore`
Capitalizes a string using `Ember.String.underscore`.
```hbs
{{underscore "whatsThat"}}
{{underscore phrase}}
```
**[⬆️ back to top](#available-helpers)**
#### `w`
Splits a string on whitespace and/or turns multiple words into an array
Expand Down
12 changes: 12 additions & 0 deletions addon/helpers/underscore.js
@@ -0,0 +1,12 @@
import Ember from 'ember';
const {
Helper: { helper },
String: { underscore: _underscore }
} = Ember;

export function underscore([string]) {
string = string || '';
return _underscore(string);
}

export default helper(underscore);
1 change: 1 addition & 0 deletions addon/index.js
Expand Up @@ -18,5 +18,6 @@ export { default as RepeatHelper } from './helpers/repeat';
export { default as SortByHelper } from './helpers/sort-by';
export { default as TakeHelper } from './helpers/take';
export { default as ToggleHelper } from './helpers/toggle';
export { default as UnderscoreHelper } from './helpers/underscore';
export { default as UnionHelper } from './helpers/union';
export { default as WHelper } from './helpers/w';
1 change: 1 addition & 0 deletions app/helpers/underscore.js
@@ -0,0 +1 @@
export { default, underscore } from 'ember-composable-helpers/helpers/underscore';
46 changes: 46 additions & 0 deletions tests/integration/helpers/underscore-test.js
@@ -0,0 +1,46 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('underscore', 'Integration | Helper | {{underscore}}', {
integration: true
});

test('It converts camelCase correctly', function(assert) {
this.render(hbs`{{underscore "andAnother"}}`);

let expected = 'and_another';

assert.equal(this.$().text().trim(), expected, 'converts camelCase to underscored');
});

test('It converts dashes to underscores', function(assert) {
this.render(hbs`{{underscore "harry-potter"}}`);

let expected = 'harry_potter';

assert.equal(this.$().text().trim(), expected, 'converts dashes to underscores');
});

test('It converts spaces to underscores', function(assert) {
this.render(hbs`{{underscore "age is foolish and forgetful when it underestimates youth"}}`);

let expected = 'age_is_foolish_and_forgetful_when_it_underestimates_youth';

assert.equal(this.$().text().trim(), expected, 'converts spaces to underscores');
});

test('It correctly handles empty string input', function(assert) {
this.render(hbs`{{underscore ""}}`);

let expected = '';

assert.equal(this.$().text().trim(), expected, 'renders empty string if input is empty string');
});

test('It correctly handles undefined input', function(assert) {
this.render(hbs`{{underscore undefined}}`);

let expected = '';

assert.equal(this.$().text().trim(), expected, 'renders empty string if undefined input');
});

0 comments on commit d56d20c

Please sign in to comment.