Skip to content

Commit

Permalink
create constants file for special array strings (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden authored Jul 7, 2017
1 parent ee4b9ae commit 7c19767
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions addon/-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ARRAY_EACH = '@each.';
export const ARRAY_LENGTH = '[]';
8 changes: 6 additions & 2 deletions addon/collapse-key.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import expandProperty from './expand-property';
import {
ARRAY_EACH,
ARRAY_LENGTH
} from './-constants';

export default function(property) {
if (typeof property !== 'string') {
return [property];
}

let arrayIndex = property.indexOf('@each.');
let arrayIndex = property.indexOf(ARRAY_EACH);
if (arrayIndex === -1) {
arrayIndex = property.indexOf('[]');
arrayIndex = property.indexOf(ARRAY_LENGTH);
}

if (arrayIndex === 0) {
Expand Down
6 changes: 5 additions & 1 deletion addon/create-class-computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import WeakMap from 'ember-weakmap';
import getValue from './get-value';
import { collapseKeysWithMap } from './collapse-keys';
import flattenKeys from './flatten-keys';
import {
ARRAY_EACH,
ARRAY_LENGTH
} from './-constants';

const { defineProperty } = Ember;

Expand Down Expand Up @@ -72,7 +76,7 @@ export default function(observerBools, macroGenerator) {
function getOriginalArrayDecorator(key, i) {
if (typeof key === 'string') {
let originalKey = keys[keyMap[i]];
if (originalKey.indexOf('[]') !== -1 || originalKey.indexOf('@each.') !== -1) {
if (originalKey.indexOf(ARRAY_EACH) !== -1 || originalKey.indexOf(ARRAY_LENGTH) !== -1) {
return originalKey;
}
}
Expand Down
12 changes: 8 additions & 4 deletions addon/normalize-array-key.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { isBlank } from '@ember/utils';
import {
ARRAY_EACH,
ARRAY_LENGTH
} from './-constants';

export default function(array, keys = []) {
// this macro support should be extracted out
Expand All @@ -9,7 +13,7 @@ export default function(array, keys = []) {

let props;

let i = array.indexOf('@each.');
let i = array.indexOf(ARRAY_EACH);
if (i !== -1) {
let chain = array.split('.');
let end = chain[chain.length - 1];
Expand All @@ -19,7 +23,7 @@ export default function(array, keys = []) {
props = [end];
}
} else {
i = array.indexOf('[]');
i = array.indexOf(ARRAY_LENGTH);
props = [];
}

Expand All @@ -44,9 +48,9 @@ export default function(array, keys = []) {

let suffix;
if (props.length === 0) {
suffix = '[]';
suffix = ARRAY_LENGTH;
} else {
suffix = '@each.';
suffix = ARRAY_EACH;
if (props.length === 1) {
suffix += props[0];
} else {
Expand Down

0 comments on commit 7c19767

Please sign in to comment.