Skip to content

Commit e36c196

Browse files
author
Vlad Balin
authored
Merge pull request #35 from Volicon/f/safe-paths-bug
fix for empty values in id-reference paths
2 parents 385bf10 + 913f169 commit e36c196

File tree

8 files changed

+28
-18
lines changed

8 files changed

+28
-18
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/traversable.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/traversable.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "type-r",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"description": "Reactive serializable data layer for modern JS applications",
55
"main": "./dist/index.js",
66
"jsnext:main": "./lib/index.js",

src/traversable.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Traversable {
1212
get( key : string ) : any
1313
}
1414

15-
const referenceMask = /\^|([^.]+)/g;
15+
const referenceMask = /\^|(store\.[^.]+)|([^.]+)/g;
1616

1717
// Compile reference to function
1818
export type ResolveReference = ( root : Traversable ) => any;
@@ -26,19 +26,27 @@ export class CompiledReference {
2626
const path = reference
2727
.match( referenceMask )
2828
.map( key => {
29-
if( key === '^' ) return 'getOwner()';
29+
if( key === '^' || key === 'owner' ) return 'getOwner()';
3030

3131
if( key[ 0 ] === '~' ) return `getStore().get("${ key.substr( 1 ) }")`;
32+
33+
if( key.indexOf( 'store.' ) === 0 ) return `getStore().get("${ key.substr( 6 ) }")`;
3234

3335
return key;
3436
} );
3537

3638
this.tail = splitTail && path.pop();
3739
this.local = !path.length;
38-
39-
path.unshift( 'self' );
4040

41-
this.resolve = <any> new Function( 'self', `return ${ path.join('.') };` );
41+
this.resolve = <any> new Function( 'self', `
42+
var v = self.${ path.shift() };
43+
44+
${ path.map( x => `
45+
v = v && v.${ x };
46+
`).join('')}
47+
48+
return v;
49+
` );
4250
}
4351
}
4452

tests/typescript/dist/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,23 +867,24 @@ var ValidationError = (function () {
867867
return ValidationError;
868868
}());
869869

870-
var referenceMask = /\^|([^.]+)/g;
870+
var referenceMask = /\^|(store\.[^.]+)|([^.]+)/g;
871871
var CompiledReference = (function () {
872872
function CompiledReference(reference, splitTail) {
873873
if (splitTail === void 0) { splitTail = false; }
874874
var path = reference
875875
.match(referenceMask)
876876
.map(function (key) {
877-
if (key === '^')
877+
if (key === '^' || key === 'owner')
878878
return 'getOwner()';
879879
if (key[0] === '~')
880880
return "getStore().get(\"" + key.substr(1) + "\")";
881+
if (key.indexOf('store.') === 0)
882+
return "getStore().get(\"" + key.substr(6) + "\")";
881883
return key;
882884
});
883885
this.tail = splitTail && path.pop();
884886
this.local = !path.length;
885-
path.unshift('self');
886-
this.resolve = new Function('self', "return " + path.join('.') + ";");
887+
this.resolve = new Function('self', "\n var v = self." + path.shift() + ";\n \n " + path.map(function (x) { return "\n v = v && v." + x + ";\n "; }).join('') + "\n\n return v;\n ");
887888
}
888889
return CompiledReference;
889890
}());

tests/typescript/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)