diff --git a/src/index.html b/src/index.html index 896a9f4..c02c190 100644 --- a/src/index.html +++ b/src/index.html @@ -197,15 +197,13 @@ app.bootstrap(document.getElementById('rootView')); var cmd = app.get('cmd'); + var hashArgs = app.get('hashArgs'); - cmd.execute('help'); - - if(window.location.hash.length > 1) { - - cmd.execute(app.get('hash').decodeHash(window.location.hash)); + if(hashArgs.commands.length > 0) { + hashArgs.commands.forEach(cmd.execute.bind(cmd)); } else { - + cmd.execute('help'); cmd.execute('1|2'); cmd.execute('1<<0x2a'); cmd.execute('2 4 8 16 32'); diff --git a/src/js/app/services.js b/src/js/app/services.js index 9bf6f9f..397e185 100644 --- a/src/js/app/services.js +++ b/src/js/app/services.js @@ -13,8 +13,48 @@ }, decodeHash: function(hashValue) { return decodeURI(hashValue).replace(/^\#/, '').replace(/,/g,' '); + }, + getArgs: function (hashValue) { + + core.should.beString(hashValue, 'hashValue'); + + var decodedHash = this.decodeHash(hashValue), + args = { + commands: [] + }; + + splitHashList(decodedHash).forEach(function(value) { + if(/^\-[a-zA-Z]+$/.test(value)) { + args[value.substr(1)] = true; + return; + } + + args.commands.push(value); + }); + + return Object.freeze(args); } + }; + + function splitHashList(str) { + var values = []; + + if(str.indexOf('||')) { + str.split('||').forEach(function (v) { + if (v.length > 0) { + values.push(v); + } + }); + } else { + values.push(str); + } + + return values; } + }); + + app.set('hashArgs', function() { + return app.get('hash').getArgs(window.location.hash); }) })(window.app, window.core); \ No newline at end of file diff --git a/tests/unit/core/htmlSpec.js b/tests/unit/core/htmlSpec.js index 2c3d602..990421f 100644 --- a/tests/unit/core/htmlSpec.js +++ b/tests/unit/core/htmlSpec.js @@ -9,7 +9,7 @@ describe('html templates', function () { }); it('should support each', function () { - var t = '{foreach n in m.lst}{each c in m.lst2}{n}{c}{/}{/}'; + var t = '{each n in m.lst}{each c in m.lst2}{n}{c}{/}{/}'; var compiled = html.compileTemplate(t); var result = compiled({lst:[1,2,3], lst2:['a','b']}); console.log(result); diff --git a/tests/unit/shell/hashSpec.js b/tests/unit/shell/hashSpec.js new file mode 100644 index 0000000..15c0c98 --- /dev/null +++ b/tests/unit/shell/hashSpec.js @@ -0,0 +1,50 @@ +describe('hash arguments parser', function() { + var hash = app.get('hash'); + + it('should parse empty', function() { + var args = hash.getArgs(''); + expect(args).not.toBe(null); + expect(args).toBeDefined(); + expect(args.commands).toEqual([]); + }); + + it('should parse single command', function() { + var args = hash.getArgs('#cmd'); + expect(args).not.toBe(null); + expect(args).toBeDefined(); + expect(args.commands).toEqual(['cmd']); + }); + + it('should parse multiple commands', function() { + var args = hash.getArgs('#1|2||1^2||~2'); + expect(args).not.toBe(null); + expect(args).toBeDefined(); + expect(args.commands).toEqual(['1|2', '1^2', '~2']); + }); + + it('should parse multiple commands url encoded', function() { + var args = hash.getArgs('#' + encodeURI('1|2||1^2||~2')); + expect(args).not.toBe(null); + expect(args).toBeDefined(); + expect(args.commands).toEqual(['1|2', '1^2', '~2']); + }); + + it('should parse multiple commands and switcher encoded', function() { + var args = hash.getArgs('#' + encodeURI('1|2||1^2||~2||-notrack||-debug')); + expect(args).not.toBe(null); + expect(args).toBeDefined(); + expect(args.commands).toEqual(['1|2', '1^2', '~2']); + expect(args.notrack).toBe(true); + expect(args.debug).toBe(true); + }); + + it('should parse only switchers encoded', function() { + var args = hash.getArgs('#' + encodeURI('-notrack||-debug')); + expect(args).not.toBe(null); + expect(args).toBeDefined(); + expect(args.commands).toEqual([]); + expect(args.notrack).toBe(true); + expect(args.debug).toBe(true); + }); + +}); \ No newline at end of file