File: components/webfan/cli/clisetup/clisetup.js

Recommend this page to a friend!
  Classes of Till Wehowski   µ.Flow   components/webfan/cli/clisetup/clisetup.js   Download  
File: components/webfan/cli/clisetup/clisetup.js
Role: Class source
Content type: text/plain
Description: Class source
Class: µ.Flow
General purpose library of objects
Author: By
Last change:
Date: 7 years ago
Size: 8,841 bytes
 

Contents

Class file image Download
'use strict'; var config = {}; config.sqlstarts = [ 'create', 'rollback', 'begin', 'commit', 'alter', 'select', 'update', 'delete', 'create', 'insert', 'show', 'replace', 'drop', 'explain' ]; config.default_options = { debug : false, defaults : { standards : true, logs : true, onceFinalize : true, firstArg : true, sql : true } }; /** Taken from: http://krasimirtsonev.com/blog/article/Simple-command-line-parser-in-JavaScript not regarding qoutes CommandParser.parse('command param1 "that\'s a long parameter" param3'); ["command", "param1", ""that's", "a", "long", "parameter"", "param3"] regarding quotes CommandParser.parse('command param1 "that\'s a long parameter" param3', true); ["command", "param1", "that's a long parameter", "param3"] CommandParser.parse('command param1 "that\'s a long parameter" "param3 is also a string"', true); ["command", "param1", "that's a long parameter", "param3 is also a string"] */ var CommandParser = (function() { var parse = function(str, lookForQuotes) { var args = []; var readingPart = false; var part = ''; for(var i=0; i<str.length; i++) { if(str.charAt(i) === ' ' && !readingPart) { if(''!==part)args.push(part); part = ''; } else { if(!!lookForQuotes && (str.charAt(i) === '\"' || str.charAt(i) === "\'")) { readingPart = !readingPart; } else { part += str.charAt(i); } } } if(''!==part)args.push(part); return args; } return { parse: parse } })(); config.parseArgs = CommandParser.parse; config.substr =function(str, start, len) { /*/ discuss at: http://locutus.io/php/substr/ // original by: Martijn Wieringa // bugfixed by: T.Wild // improved by: Onno Marsman (https://twitter.com/onnomarsman) // improved by: Brett Zamir (http://brett-zamir.me) // revised by: Theriault (https://github.com/Theriault) // note 1: Handles rare Unicode characters if 'unicode.semantics' ini (PHP6) is set to 'on' // example 1: substr('abcdef', 0, -1) // returns 1: 'abcde' // example 2: substr(2, 0, -6) // returns 2: false // example 3: ini_set('unicode.semantics', 'on') // example 3: substr('a\uD801\uDC00', 0, -1) // returns 3: 'a' // example 4: ini_set('unicode.semantics', 'on') // example 4: substr('a\uD801\uDC00', 0, 2) // returns 4: 'a\uD801\uDC00' // example 5: ini_set('unicode.semantics', 'on') // example 5: substr('a\uD801\uDC00', -1, 1) // returns 5: '\uD801\uDC00' // example 6: ini_set('unicode.semantics', 'on') // example 6: substr('a\uD801\uDC00z\uD801\uDC00', -3, 2) // returns 6: '\uD801\uDC00z' // example 7: ini_set('unicode.semantics', 'on') // example 7: substr('a\uD801\uDC00z\uD801\uDC00', -3, -1) // returns 7: '\uD801\uDC00z' // test: skip-3 skip-4 skip-5 skip-6 skip-7 */ str += ''; var end = str.length; /* var iniVal = (typeof require !== 'undefined' ? require('../info/ini_get')('unicode.emantics') : undefined) || 'off'; */ var iniVal = 'on'; if (iniVal === 'off') { if (start < 0) { start += end; } if (typeof len !== 'undefined') { if (len < 0) { end = len + end; } else { end = len + start; } } if (start >= str.length || start < 0 || start > end) { return false; } return str.slice(start, end); } var i = 0; var allBMP = true; var es = 0; var el = 0; var se = 0; var ret = ''; for (i = 0; i < str.length; i++) { if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) { allBMP = false; break; } } if (!allBMP) { if (start < 0) { for (i = end - 1, es = (start += end); i >= es; i--) { if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) { start--; es--; } } } else { var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; while ((surrogatePairs.exec(str)) !== null) { var li = surrogatePairs.lastIndex; if (li - 2 < start) { start++ } else { break; } } } if (start >= end || start < 0) { return false; } if (len < 0) { for (i = end - 1, el = (end += len); i >= el; i--) { if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) { end--; el--; } } if (start > end) { return false; } return str.slice(start, end) } else { se = start + len; for (i = start; i < se; i++) { ret += str.charAt(i); if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) { se++; } } return ret; } } }; config.stripslashes=function(str) { /*/ discuss at: http://locutus.io/php/stripslashes/ // original by: Kevin van Zonneveld (http://kvz.io) // improved by: Ates Goral (http://magnetiq.com) // improved by: marrtins // improved by: rezna // fixed by: Mick@el // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) // bugfixed by: Brett Zamir (http://brett-zamir.me) // input by: Rick Waldron // input by: Brant Messenger (http://www.brantmessenger.com/) // reimplemented by: Brett Zamir (http://brett-zamir.me) // example 1: stripslashes('Kevin\'s code') // returns 1: "Kevin's code" // example 2: stripslashes('Kevin\\\'s code') // returns 2: "Kevin\'s code" */ return (str + '') .replace(/\\(.?)/g, function (s, n1) { switch (n1) { case '\\': return '\\'; case '0': return '\u0000'; case '': return ''; default: return n1; } }); }; config.escapeshellarg=function(arg) { /*/ discuss at: http://locutus.io/php/escapeshellarg/ // original by: Felix Geisendoerfer (http://www.debuggable.com/felix) // improved by: Brett Zamir (http://brett-zamir.me) // example 1: escapeshellarg("kevin's birthday") // returns 1: "'kevin\\'s birthday'" */ var ret = ''; ret = arg.replace(/[^\\]'/g, function (m, i, s) { return m.slice(0, 1) + '\\\''; }); return "'" + ret + "'"; }; config.ql_escape_string=function(arg) { /*/ discuss at: http://locutus.io/php/escapeshellarg/ // original by: Felix Geisendoerfer (http://www.debuggable.com/felix) // improved by: Brett Zamir (http://brett-zamir.me) // example 1: escapeshellarg("kevin's birthday") // returns 1: "'kevin\\'s birthday'" */ if('string'===typeof arg && 'function' === typeof arg.replace){ var ret = ''; ret = arg.replace(/[^\\]'/g, function (m, i, s) { return m.slice(0, 1) + '\\\''; }); return "'" + ret + "'"; }else if('string'===typeof arg){ return "'" + arg + "'"; }else{ return arg; /* return "'" + arg + "'"; */ } }; config.trim=function(str, charlist) { /*/ discuss at: http://locutus.io/php/trim/ // original by: Kevin van Zonneveld (http://kvz.io) // improved by: mdsjack (http://www.mdsjack.bo.it) // improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev) // improved by: Kevin van Zonneveld (http://kvz.io) // improved by: Steven Levithan (http://blog.stevenlevithan.com) // improved by: Jack // input by: Erkekjetter // input by: DxGx // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) // example 1: trim(' Kevin van Zonneveld ') // returns 1: 'Kevin van Zonneveld' // example 2: trim('Hello World', 'Hdle') // returns 2: 'o Wor' // example 3: trim(16, 1) // returns 3: '6' */ var whitespace = [ ' ', '\n', '\r', '\t', '\f', '\x0b', '\xa0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200a', '\u200b', '\u2028', '\u2029', '\u3000' ].join(''); var l = 0; var i = 0; str += ''; if (charlist) { whitespace = (charlist + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^:])/g, '$1'); } l = str.length; for (i = 0; i < l; i++) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.substring(i); break; } } l = str.length; for (i = l - 1; i >= 0; i--) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.substring(0, i + 1); break; } } return whitespace.indexOf(str.charAt(0)) === -1 ? str : ''; }; exports = module.exports = config;