Skip to content

Instantly share code, notes, and snippets.

@jacobq
Last active June 22, 2017 20:51
Show Gist options
  • Save jacobq/a716e4104aa3bf975df2285eb44831c9 to your computer and use it in GitHub Desktop.
Save jacobq/a716e4104aa3bf975df2285eb44831c9 to your computer and use it in GitHub Desktop.
Array.any Example
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
testArrayAny: Ember.computed(function() {
let emptyArray = Ember.A();
let singleValuedArray = Ember.A([true]);
let specialArray = Ember.A([null, undefined, NaN]);
let normalArray = Ember.A([0,1,2,3]);
let cases = [{
name: 'true if even; false otherwise',
f: x => x % 2 === 0
}, {
name: '1 if even; 0 otherwise',
f: x => x % 2 === 0
}, {
name: 'true if odd; false otherwise',
f: x => x % 2 === 1
}, {
name: '1 if odd; 0 otherwise',
f: x => (x % 2 === 1) ? 1 : 0
}, {
name: 'Always true',
f: x => true
}, {
name: 'Always false',
f: x => false
}, {
name: 'Always null (falsey)',
f: x => null
}, {
name: 'Always 0 (falsey)',
f: x => 0
}, {
name: 'Always 1 (truthy)',
f: x => 1
}, {
name: 'Identity',
f: x => x
}];
return cases.map(({name, f}) => {
return {
name: name,
empty: emptyArray.any(f),
single: singleValuedArray.any(f),
special: specialArray.any(f),
normal: normalArray.any(f)
};
});
})
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
table {
width: 100%;
}
th {
text-align: left;
}
<table>
<thead>
<tr>
<th>f returns...</th><th>[].any(f)</th>
<th>[true].any(f)</th>
<th>[null, undefined, NaN].any(f)</th>
<th>[0,1,2,3].any(f)</th>
</tr>
</thead>
<tbody>
{{#each testArrayAny as |result|}}
<tr>
<td>{{result.name}}</td>
<td>{{result.empty}}</td>
<td>{{result.single}}</td>
<td>{{result.special}}</td>
<td>{{result.normal}}</td>
</tr>
{{/each}}
</tbody></table>
<br>
<br>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment