taking off the http or https off a javascript string
Try with this: var url = “https://site.com”; var urlNoProtocol = url.replace(/^https?\:\/\//i, “”);
Try with this: var url = “https://site.com”; var urlNoProtocol = url.replace(/^https?\:\/\//i, “”);
Here’s a useful trick 🙂 function randomWithProbability() { var notRandomNumbers = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]; var idx = Math.floor(Math.random() * notRandomNumbers.length); return notRandomNumbers[idx]; }
Well… It was a little bit difficult but mixing all answers in this post I made it work. Firstly, you should create a transform for the new type “array”: DS.ArrayTransform = DS.Transform.extend({ deserialize: function(serialized) { return (Ember.typeOf(serialized) == “array”) ? serialized : []; }, serialize: function(deserialized) { var type = Ember.typeOf(deserialized); if (type == ‘array’) … Read more
I have a one-liner similar to Marc’s but with a simpler Regular Expression and ~20% faster according my benchmark (Chrome 89). const kebabize = (str) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? “-” : “”) + $.toLowerCase()) const words = [‘StackOverflow’, ‘camelCase’, ‘alllowercase’, ‘ALLCAPITALLETTERS’, ‘CustomXMLParser’, ‘APIFinder’, ‘JSONResponseData’, ‘Person20Address’, ‘UserAPI20Endpoint’]; console.log(words.map(kebabize)); [A-Z]+(?![a-z]) matches any consecutive capital … Read more
CAUSE When DataTables destroys the table because of the option destroy:true it restores original content and discards the content that you’ve generated. SOLUTION #1 Remove destroy:true option and destroy the table before you manipulate the table with destroy() API method. if ( $.fn.DataTable.isDataTable(‘#tblRemittanceList’) ) { $(‘#tblRemittanceList’).DataTable().destroy(); } $(‘#tblRemittanceList tbody’).empty(); // … skipped … $(‘#tblRemittanceList’).dataTable({ “autoWidth”:false, … Read more
TCO, or rather, Tail Call Elimination in JavaScript — also often referred to as Proper Tail Calls (PTC) in discussions — is a long and sad story. Around 2011, TC39 (the JavaScript standards committee) decided to adopt mandatory TCE for the forthcoming ES6 standard, with consensus from all major browser vendors. In 2015, the new … Read more
I’d highly suggest giving Code Complete 2 by Steve McConnell a read. His argument is that you should neither declare all of your variables in one line, nor should should declare them all at the top of a routine. So, don’t do this: function foo() { var a, b, c, d; /** * 20 lines … Read more
function random_rgba() { var o = Math.round, r = Math.random, s = 255; return ‘rgba(‘ + o(r()*s) + ‘,’ + o(r()*s) + ‘,’ + o(r()*s) + ‘,’ + r().toFixed(1) + ‘)’; } var color = random_rgba(); g.fillStyle = color; g.strokeStyle = color; FIDDLE
You can use this JavaScript\jQuery code: // Sets active link in Bootstrap menu // Add this code in a central place used\shared by all pages // like your _Layout.cshtml in ASP.NET MVC for example $(‘a[href=”‘ + this.location.pathname + ‘”]’).parents(‘li,ul’).addClass(‘active’); It’ll set the <a>‘s parent <li> and the <li>‘s parent <ul> as active. A simple solution … Read more
You can use .slice(): > var a = [1, 2, 3, 4, 5]; > [a.slice(0, -1).join(‘, ‘), a.slice(-1)[0]].join(a.length < 2 ? ” : ‘ and ‘); ‘1, 2, 3, 4 and 5’ a.slice(0, -1).join(‘, ‘): takes all but the last element and joins them together with a comma. a.slice(-1)[0]: it’s the last element. .join(a.length < … Read more