Skip to content

Instantly share code, notes, and snippets.

View gadanihiman's full-sized avatar
🎯
Work From Anywhere

Gadani Himan Gurusinga gadanihiman

🎯
Work From Anywhere
View GitHub Profile
@gadanihiman
gadanihiman / StringHelpers.ts
Created August 16, 2024 05:00
List helper for string in typescript
export const convertCamelCaseToSnakeCase = (str: string) => {
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
}
export const convertSnakeCaseToCamelCase = (str: string) => {
return str.replace(/([-_][a-z])/g, (group) =>
group.toUpperCase().replace('-', '').replace('_', '')
)
}
@gadanihiman
gadanihiman / enzyme_render_diffs.md
Created January 4, 2021 07:09 — forked from fokusferit/enzyme_render_diffs.md
Difference between Shallow, Mount and render of Enzyme

Shallow

Real unit test (isolation, no children render)

Simple shallow

Calls:

  • constructor
  • render
@gadanihiman
gadanihiman / uploadS3.js
Created September 8, 2020 13:34
Upload File With S3 AWS
const fs = require('fs');
const AWS = require('aws-sdk');
// Enter copied or downloaded access id and secret here
const ID = '';
const SECRET = '';
// Enter the name of the bucket that you have created here
const BUCKET_NAME = 'test-bucket-1242tsr';;
@gadanihiman
gadanihiman / fileToBase64.js
Created April 6, 2020 09:08
Convert File to Base64
export const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const b64 = reader.result.replace(/^data:.+;base64,/, '');
resolve(b64)
};
reader.readAsDataURL(file);
reader.onerror = error => reject(error);
});
@gadanihiman
gadanihiman / gist:45227f9607218d76f5ac4a0faf4e80f3
Created April 6, 2020 07:26
Find Second Great Low Number
const filterDuplicateArr = (arr) => [...new Set(arr)];
const findSecondGreatestNum = (arr = [1,2]) => {
const filteredDuplicateArr = filterDuplicateArr(arr)
const firstMax = Math.max(...filteredDuplicateArr);
//remove the first max from array
filteredDuplicateArr.shift(filteredDuplicateArr.indexOf(firstMax));
// return the new max
return Math.max(...filteredDuplicateArr);
}
@gadanihiman
gadanihiman / SortedSmallToBig.js
Created March 2, 2020 08:01
Sorted Small To Big
const input = [5, 2, 10, 3, 11, -1, 0];
const sortedInput = [];
for (let i = 0; i < input.length; i++) {
for (let j = i; j < input.length; j++) {
console.log('input[i]', input[i]);
console.log('input[j]', input[j]);
if (input[i] > input[j]) {
let tmpNum = input[i];
@gadanihiman
gadanihiman / PrintStarCase.js
Last active March 1, 2020 17:34
Print Hash Case Align Right.
// Hackerrank
// https://www.hackerrank.com/challenges/staircase/problem
function staircase(n) {
for(let i = 1, j = n-1; i <= n; i++, j--){
const space = ' '.repeat(j);
console.log(space+'#'.repeat(i));
}
}
@gadanihiman
gadanihiman / MatrixDiagonal.js
Last active March 1, 2020 07:59
Finding Matrix Diagonal Difference
// https://www.hackerrank.com/challenges/diagonal-difference/problem
// first way
function diagonalDifference(arr) {
let sum1 = 0;
let sum2 = 0;
for(let i = 0, j = arr[0].length-1; i < arr[0].length; i++, j--){
sum1 += arr[i][i];
sum2 += arr[i][j];
@gadanihiman
gadanihiman / findMaxNinAnArray.js
Last active February 22, 2020 11:45
Find Max N in an Array
//Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
// Output: [3,3,5,5,6,7]
// Explanation:
// Window position Max
// --------------- -----
// [1 3 -1] -3 5 3 6 7 3
// 1 [3 -1 -3] 5 3 6 7 3
// 1 3 [-1 -3 5] 3 6 7 5
// 1 3 -1 [-3 5 3] 6 7 5
var emitter = require('events').EventEmitter;
function LoopProcessor(num) {
var e = new emitter();
setTimeout(function () {
for (var i = 1; i <= num; i++) {
e.emit('BeforeProcess', i);