Skip to content

Instantly share code, notes, and snippets.

@agnel
agnel / useObjectState.d.ts
Created March 9, 2022 13:32
useObjectState hook in react with type definitions
import { Dispatch, SetStateAction } from 'react';
/**
* This method needs to provide the type of the initialState like
* `useObjectState<initialStateInterface>(initialState)`
* @param {*} initialState The initial state the user wants to set
* @returns Array containing `state` object, `setState` method and `updateState` method
*/
function useObjectState<S>(initialState: S): [
S,
@agnel
agnel / useNetwork.ts
Created March 8, 2022 11:56
useNetwork Hook in react
import { useState, useEffect } from 'react';
function getNetworkConnection() {
return (
window.navigator.connection
);
}
function getNetworkConnectionInfo() {
const connection = getNetworkConnection();
@agnel
agnel / __mixins.js
Last active March 19, 2018 06:15
A collection of _.js mixins
_.mixin({
isHash: function(object) {
return _.isObject(object) && !_.isArray(object);
},
capitalize: function(string) {
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
},
@agnel
agnel / load_libs_in_console.js
Last active September 13, 2017 06:47
load any js library into developers console using fetch
// load angular
fetch('https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js')
.then(response => response.text())
.then(text => eval(text))
// load $
fetch('http://code.jquery.com/jquery-2.1.0.js')
.then(response => response.text())
.then(text => eval(text))
@agnel
agnel / multiple_ajax_request.js
Created June 9, 2017 09:04
Multiple Ajax requests using jQuery.when
var requests = [];
var responses = [];
var urls = []; // sample urls
$.each(urls, function(url) {
requests.push( $.get(url, getQueryParams(), function(resp){ responses.push(resp) }) );
});
// here this is the object to which the arguments is to be passed
@agnel
agnel / egghead-lesson-urls-code.js
Last active June 9, 2017 06:07
Extract Egghead.io Lesson urls from the DOM
/**
* Paste this in the console of a lesson page
*
* @return [Array] List of urls for a course
*/
$.map($('.up-next-list-item'), function(el) { return $(el).attr('href') });
/**
* Paste this on the courses page
* Tip: convert the list to String by append .join('\n')
@agnel
agnel / simple_sort.rb
Created October 5, 2016 13:31 — forked from emad-elsaid/simple_sort.rb
simple sorting algorithms with ruby this is a small practice for implementing simple sorting algorithms using ruby, i grabed the Pseudocode from their respective wikipedia pages and converted them to ruby, one new thing i have learned from this simple practice is swapping arrays elements in single line of code.
#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)
#
# this script is a small practice in implementing
# simple sorting algorithms in ruby, i converted
# the sorting algorithms from wikipedia pages
class Array
# Insertion sort is a simple sorting algorithm that builds
# the final sorted array (or list) one item at a time.
@agnel
agnel / contiguous_subarray.rb
Last active September 13, 2016 13:06
Ruby Program to find contiguous subarray within a one dimensional array of numbers (containing at least one positive number) which has the largest sum
# Question:
# Write a RoR program to find the contiguous subarray within a onedimensional
# array of numbers (containing at least one positive number) which has the largest sum.
# For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous
# subarray with the largest sum is 4, −1, 2, 1, with sum 6.
# The program should run as follows <bold is user entry>:
# Enter the array :
# -2 1 -3 4 -1 2 1 -5 4
# => [-2, 1, -3, 4, -1, 2, 1, -5, 4]
# Largest SubArray
@agnel
agnel / Guardfile
Last active May 2, 2019 14:15
Setting up MiniTest, RSpec, Spring and Guardfile
guard :minitest, spring: true, all_on_start: false do
# with Minitest::Unit
watch(%r{^test/(.*)\/?test_(.*)\.rb$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
watch(%r{^test/test_helper\.rb$}) { 'test' }
watch('config/routes.rb') { 'test' }
watch(%r{^app/models/(.*?)\.rb$}) do |matches|
"test/models/#{matches[1]}_test.rb"
end
watch('app/views/layouts/application.html.erb') do
@agnel
agnel / marray.rb
Created February 16, 2016 10:35 — forked from borowskiio/marray.rb
Produce PHP-style multidimensional array, e.g arr[1][2][3] = 'foobar'
# Produce PHP-style multidimensional array.
#
# Example
#
# arr = Marray.new
#
# arr[1][2][3] = "foo"
# => "foo"
#
# arr[1][2][3]