Skip to content

Instantly share code, notes, and snippets.

View kosperera's full-sized avatar
🏠
Working from home

Kosala (Nuwan) Perera kosperera

🏠
Working from home
View GitHub Profile
@kosperera
kosperera / check-grades.js
Last active October 31, 2021 13:49
Using JS conditionals
function ifGrade(marks) {
if (marks > 100) return 'Cheating is bad!';
else if (marks >= 90) return 'A+';
else if (marks >= 80) return 'A';
else if (marks >= 70) return 'B+';
else if (marks >= 60) return 'B';
else if (marks >= 50) return 'C';
else if (marks >= 40) return 'D';
else return 'F';
}
@kosperera
kosperera / vanillajs-kebabcase.md
Last active August 28, 2020 11:15
Vanilla JS Kebab Case

kebabCase

You want to have a look at LoDash or Underscore or Slugify for more reliable version. This is just for fun, and probably might not suitable for any production use 🤓

So, I wanted to kebabCase a string literal without having to refer any of the aboves. After reading few js libraries mentioned above, this is what I ended up.

function kebabCase(value, separator = '_') {
  // Expression to trim trailings
  var defaultToWS = '[' + separator.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1') + ']';
@kosperera
kosperera / awesome-git.md
Last active November 14, 2023 05:04
Awesome Git Commands

Hard Reset Local branch to the origin/remote.

This basically discard all changes, commits, etc. which are not pushed to remote yet, and pull the latest version from the remote.

git fetch origin master
git reset --hard FETCH_HEAD
git clean -df

Managing sub-module repos.

@kosperera
kosperera / show-ga-data.js
Last active April 30, 2019 08:13
Show Google Analytics Tracker data.
function showmethemoney(asjson) {
var a = ga.getAll();
a.forEach(function(g) {
var json = {
gid: g.get('_gid'),
trackingId: g.get('trackingId'),
clientId: g.get('clientId'),
cookieDomain: g.get('cookieDomain'),
location: g.get('location'),
data: JSON.parse(JSON.stringify(g.b.data.values))
@kosperera
kosperera / .npmrc
Last active June 7, 2019 10:09
Clean code style guide examples.
save=true
save-exact=true
loglevel=error
package-lock=false
@kosperera
kosperera / missing-indexes-on-tables.sql
Created March 27, 2018 12:09
Find out missing indexes for queries running on the database.
SELECT TOP 20
total_worker_time/execution_count AS Avg_CPU_Time
,Execution_count
,total_elapsed_time/execution_count as AVG_Run_Time
,total_elapsed_time
,(SELECT
SUBSTRING(text,statement_start_offset/2+1,statement_end_offset
) FROM sys.dm_exec_sql_text(sql_handle)
) AS Query_Text
FROM sys.dm_exec_query_stats
@kosperera
kosperera / string.reverse.js
Last active May 2, 2017 19:06
Reverse a string value in JavaScript.
String.prototype.reverse = () => {
return this.split('').reverse().join('');
};
@kosperera
kosperera / selfystic-crash-course.md
Last active November 12, 2016 15:43
Selfystic ionic crash course

Selfystic crash course gists

Playground so we don't have to install anything, I meant, ANYTHING! Ionic is all about Angular and Cordova.

angularjs

<!-- index.html -->

<div class="list" ng-controller="Hello">
@kosperera
kosperera / fizz-buzz.kata.js
Last active August 19, 2018 07:54
Prints Fizz for multiples of 3, and Buzz and for multiples of 5, and FizzBuzz for multiples of both 3 and 5.
function goFizzBuzz(start, end) {
for (let i = start; i <= end; i++) {
if (i % 15 == 0) { console.log('FizzBuzz'); }
else if (i % 3 == 0) { console.log('Fizz'); }
else if (i % 5 == 0) { console.log('Buzz'); }
else { console.log(i); }
}
}
@kosperera
kosperera / ReverseArray.js
Created August 5, 2014 19:48
Reverse an array without using another array. This method is called Temporary Swap and it only runs for half of the array.
function reverse(arr)
{
var left = null;
var right = null;
var length = arr.length;
for (left = 0; left < length / 2; left += 1)
{
right = length - 1 - left;
var temporary = arr[left];
arr[left] = arr[right];