Skip to content

Instantly share code, notes, and snippets.

View darkcris1's full-sized avatar
🎯
Focusing

Cris Fandiño Jr. darkcris1

🎯
Focusing
  • Philippines
View GitHub Profile
@darkcris1
darkcris1 / svelte-events.js
Created August 20, 2021 14:20
An action for svelte to dynamically add listenera
export function getEventsAction() {
const component = get_current_component();
return node => {
const events = Object.keys(component.$$.callbacks);
const listeners = [];
events.forEach(
event => listeners.push(
listen(node, event, e => bubble(component, e))
)
@darkcris1
darkcris1 / numerology.js
Created February 14, 2021 12:39
Convert and add the letters into number
const letterExhange = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
@darkcris1
darkcris1 / npm-browser.js
Last active February 27, 2021 16:16
Install a npm package with udm type only in the browser
/**
* This will return true if the value is some of the items
* @param {string} pkgName
* @param {string} functionName
* @param {function} callback
*/
function npm(pkgName,functionName,callback){
const http = new XMLHttpRequest();
@darkcris1
darkcris1 / onViewport.js
Created February 7, 2021 10:56
Add a callback if the element is on the viewport
function onViewport(elem, callback, options = {}) {
if (elem instanceof Element) elem = [elem]
// Initial option
// Unobserve the element if it is on the viewport for optimizing purposes
const { abortEarly = true } = options
let counter = 0
const observer = new IntersectionObserver((entries, self) => {
entries.forEach((el) => {
@darkcris1
darkcris1 / array-string-includes-polyfill.js
Created January 27, 2021 10:46
Array.includes | String.includes Polyfill
if (!String.prototype.includes){
String.prototype.includes = function(value, fromIndex){
return this.indexOf(value, fromIndex || 0) > -1
}
}
if (!Array.prototype.includes){
Array.prototype.includes = function(value, fromIndex){
return this.indexOf(value, fromIndex || 0) > -1
}
@darkcris1
darkcris1 / nodelist-htmlcollection-polyfill.js
Created January 27, 2021 10:08
NodeList forEach polyfill for IE 11 | + HTMLCollection.forEach
// You can exclude this if you will not use it
HTMLCollection.prototype.forEach = function (callback, thisArg) {
Array.prototype.forEach.call(this, callback, thisArg || window)
}
if ('NodeList' in window && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function (callback, thisArg) {
Array.prototype.forEach.call(this, callback, thisArg || window)
}
@darkcris1
darkcris1 / uuid.js
Created January 7, 2021 10:36
Simple Unique ID Generator
function uuid(prefix = '', date = true) {
let counter = 0
return {
create: () => {
return `${date ? Date.now().toString(16) : ''}${prefix}${counter++}`
},
}
}
const uid = uuid('prefix')
console.log(uid.create()) // 176dc698f58prefix0
@darkcris1
darkcris1 / cConjecture.js
Created January 5, 2021 11:54
Sololearn cConjecture Community Challenge
function cConjecture(x) {
const result = []
while (x > 1) {
if (x % 2 === 0) {
x = x / 2
result.push(x)
} else {
x = x * 3 + 1
result.push(x)
}
@darkcris1
darkcris1 / rangeExtraction.js
Last active March 29, 2023 22:24
Range Extraction | Codewars
function solution(list) {
const result = []
let str = []
for (let i = 0; i < list.length; i++) {
if (list[i + 1] === list[i] + 1) {
str.push(list[i])
} else {
if (str.length < 2) {
result.push(...str, list[i])
} else {
@darkcris1
darkcris1 / linearSolver.js
Last active January 3, 2021 11:49
Linear Equation Solver | Sololearn
function linearSolver(equation) {
const equa = equation.split('=').map((x) => x.replace(/\s/g, ''))
const hasX = equa.find((x) => /x/.test(x))
const noX = equa.find((x) => !/x/.test(x))
let x = 0
const errorMsg = 'Invalid Equation: ' + equation
try {
if (/\b\W/.test(hasX)) {
const c = hasX.match(/(-|\+|)+(\d+x|x\d+)/)[0]