Skip to content

Instantly share code, notes, and snippets.

@sidhantpanda
sidhantpanda / debounce.ts
Created March 20, 2021 09:57
Debounce in TypeScript
/**
* Debounce wrapper for functions which are called rapidly
* This wrapper ensures that there is a minimum wait time
* between function invocations.
* @param func Function to be called
* @param wait Minimum time between function calls
* @param immediate True if function needs to be invoked immediately,
* false if needs to be invoked after delay
* @returns Debounced function with the same signature as `func`
*/
@sidhantpanda
sidhantpanda / logt-console-example.js
Last active February 4, 2020 23:30
logt-console-example
// Just copy and paste the following code in the console
var logt = createLogger(5);
function add(a, b) {
var logTag = '.add';
logt.debug(logTag, 'Adding', a, 'and', b);
var result = a + b;
logt.debug(logTag, 'Result:', result);
}
const logger = new LogT(0);
logger.warn('TAG', 'warning message'); // Will not print anything to console
logger.info('TAG', 'info message'); // Will not print anything to console
logger.debug('TAG', 'debug message'); // Will not print anything to console
logger.silly('TAG', 'silly message'); // Will not print anything to console
logger.showHidden(1); // Will print the warning message
logger.showHidden(2); // Will print the info warning message
logger.showHidden(5); // Will print the debug as well as silly message
<script src="https://cdn.jsdelivr.net/gh/sidhantpanda/logt/dist/logt.min.js"></script>
<script>
var LOG_TAG = 'sample tag';
var logger = createLogger('error');
logger.error(LOG_TAG, new Error('example error'));
</script>
@sidhantpanda
sidhantpanda / logt-example.ts
Created September 24, 2019 15:08
Example usage for logt
import LogT from "logt";
const LOG_TAG = "sample tag";
const logger = new LogT("error");
logger.error(LOG_TAG, new Error("example error"));
@sidhantpanda
sidhantpanda / partition.ts
Created September 23, 2019 21:47
Partition an array into 2 arrays in TypeScript
interface IPartitionFunction<T> {
(element: T): boolean;
}
/**
* Partitions an array in to two arrays based on `isValid` function
* @param array Array to partition
* @param isValid Function to test each item against
*/
const partition = <T>(array: T[], isValid: IPartitionFunction<T>): T[][] => {
#!/bin/bash
repoUrl=git@github.com:sidhantpanda/express-typescript-boilerplate.git
read -p "Enter project name [express-typescript-boilerplate]: " project_name
if [ "$project_name" == "" ]; then
project_name="express-typescript-boilerplate"
fi
echo -e "Generating project $project_name!\n\n"
git clone $repoUrl $project_name
@sidhantpanda
sidhantpanda / PlyrComponent.js
Created March 6, 2017 18:22
React implementation for Selz/Plyr
import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
export default class PlyrComponent extends React.Component {
static propTypes = {
'options': PropTypes.object,
'youtubeKey': PropTypes.string,
}
componentDidMount() {
@sidhantpanda
sidhantpanda / BowlingScoreSimulation.js
Last active November 1, 2016 09:16
A bowling score calculator
// Assuming input is in form of [[1,2], [2,3], [10,0]]
// Came across this problem on the internet
// More at: https://blog.emilmoe.com/bowling-score-simulation/
module.exports = {
/**
* Return a list of accumulated result for an input array of scores in different throws
* @param Array attempts the attempts in the game
* @returns {Array} Score after each round, cummulatively
*/