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 / 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 / solveExpression.js
Created January 3, 2021 11:23
Find the unknown digit | Codewars
function solveExpression(exp) {
for (let i = 0; i <= 9; i++) {
const ex = exp.replace(/\?/g, i).split('=')
if (/--/.test(ex[0])) {
ex[0] = ex[0].replace(/(-([0-9]+))/g, '($1)')
}
if (/\b00|00\b/g.test(ex.join('='))) continue
if (exp.includes(i)) continue
if (eval(ex[0]) == ex[1]) return i
}
@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 / 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 / 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 / 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 / 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 / passwordgenerator.js
Last active February 7, 2021 10:48
Simple password generator
function generatePassword(length = 50) {
if (length < 5) throw new Error('Length must be minimum of 5')
const chars = 'abcdefghijklmopqrstnuvwxyz-[]{}()*-+,./\\1234567890'
let result = ''
for (let i = 0; i < length; i++) {
const randomChars = chars[Math.floor(Math.random() * chars.length)]
result += randomChars
}
return result
@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