mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-10 06:52:05 +01:00
Implemented support of multiple commands trough hash
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
50
tests/unit/shell/hashSpec.js
Normal file
50
tests/unit/shell/hashSpec.js
Normal file
@@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user