Skip to content

Instantly share code, notes, and snippets.

@ryansukale
ryansukale / Paginator.js
Last active May 15, 2023 11:32
Pagination using urql
import { get, set } from "lodash";
import { produce } from "immer";
export default class Paginator {
path: string;
limit: number;
constructor({ path, limit }) {
this.path = path;
this.limit = limit;
}
@ryansukale
ryansukale / machine.js
Created March 11, 2023 15:23
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
export default generateTask({
method: 'create',
event_base: 'JOB',
entity: 'job',
url: resources.jobs.create
});
// The above expands to the code below
function getArrayDiff(a, b) {
var ret = [];
if (!(Array.isArray(a) && Array.isArray(b))) {
return ret;
}
var i;
var key;
for (i = a.length - 1; i >= 0; i--) {
key = a[i];
@ryansukale
ryansukale / 1_th_es6_custom_iterables.js
Last active May 29, 2016 22:58
Custom iterables in es6
var example = {one : 'something', two: 'somethingElse'};
var test = {}
test[example.one]: true;
test[example.two]: function () { console.log('so verbose');}
console.log(test.something); // true
test.somethingElse(); // Prints 'so verbose'
@ryansukale
ryansukale / PromisesExamples.js
Created June 28, 2015 01:36
Fun with promises
Promise.resolve().then(function () {
return 100;
})
.then(function() {
console.log(arguments);
});
// ---
Promise.resolve(200).then(function () {
@ryansukale
ryansukale / SassMeister-input.scss
Created December 12, 2014 21:38
Generated by SassMeister.com.
// ----
// Sass (v3.4.7)
// Compass (v1.0.1)
// ----
$du-responsive-config: (
(
table-type: ".portfolio, .options, .currencies, .commodities, .losers, .gainers, .etfs",
data-cols: ".data-col4",
min: 300,
var getData = new Promise(function(resolve, reject){
// Do something asynchronous and pass it a callback
getDataFromAMillionMilesAway(function(data){
if(data){
resolve(data);
}
else {
reject(new Error("Thats too far away"));
//Inspired by https://github.com/klughammer/node-randomstring
function generateRandomCode($length){
$str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz';
$randomChar = $str[rand(0, strlen($str)-1)];
$length= ($length == null)? 32 : $length;
$randomArray = array();
@ryansukale
ryansukale / strTrim.js
Created March 27, 2014 18:49
Trimming a string in javascript
//Reference
//http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript
if (!String.prototype.trim) {
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
String.prototype.ltrim=function(){return this.replace(/^\s+/,'');};
String.prototype.rtrim=function(){return this.replace(/\s+$/,'');};
String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};