Skip to content

Instantly share code, notes, and snippets.

View daltonmenezes's full-sized avatar
:octocat:
"Hey ho, let's code" - R4m0n3s

Dalton Menezes daltonmenezes

:octocat:
"Hey ho, let's code" - R4m0n3s
View GitHub Profile
@daltonmenezes
daltonmenezes / getTrace.js
Last active July 22, 2019 20:49
Get only path and line number of stack trace errors
const getTrace = error => error.stack.match(/(?<=\().*(?=\))/g)
@daltonmenezes
daltonmenezes / rebuildObject.js
Last active March 22, 2020 03:05
Rebuilds an object deleting an array key value from object property and keeps the rest of the old object
const tableProps = {
postId: ['varchar', 'NOT NULL', 'PRIMARY KEY'],
id: ['numeric', 'NOT NULL', 'PRIMARY KEY'],
data: ['json', 'NOT NULL']
}
const result =
Object
.keys(tableProps)
.reduce((acc, key) => {
@daltonmenezes
daltonmenezes / prepare-module-for-injection.js
Last active August 28, 2019 04:43
Convert your external module to String and catches the module body to inject using executeJavaScript on Electron
const prepareModuleForInjection = code => {
const moduleBody = /(?<=\{return\s).*(?=})|(?<=({(?![return]))).*(?=})|(?<=\=>\s)(?=[a-z]).*|(?<=\=>)\n.*/gsi
const [extractedBody] = String(code).match(moduleBody) || ''
return extractedBody
? `${extractedBody.trim()}\n\n`
: ''
}
webview.executeJavaScript(
@daltonmenezes
daltonmenezes / to-camel-case.js
Created August 28, 2019 04:50
To camelCase function for javascript.
const toCamelCase = (str, separator) =>
separator
? str.toLowerCase().replace(
new RegExp(`(${separator})*(${separator}.{1})`, 'g'),
char => char.toUpperCase().substr(1)
)
: str
console.log(
toCamelCase('my-cool-function', '-') // myCoolFunction
@daltonmenezes
daltonmenezes / minify.js
Created September 3, 2019 05:07
A function to minify strings in JavaScript.
const multilineString = `
This is...
a multiline string.
`
const minify = it => it.replace(/\s+/g, '')
minify(multilineString)
// Thisis...amultilinestring.
@daltonmenezes
daltonmenezes / symbol-iterator.example.js
Created October 9, 2019 23:07
A simple symbol iterator example
const obj = {
values: [1, 2, 3, 4, 5],
[Symbol.iterator]() {
let i = 0
return {
next: () => {
i++
return {
@daltonmenezes
daltonmenezes / pixel.js
Created March 16, 2020 03:44
Get pixel value by increment in javascript to handle element sizes
const pixel = ((init = 0) => {
let pixelValue = init
function increment(value) {
pixelValue += value
}
return {
getByIncrement: value => {
increment(value)
@daltonmenezes
daltonmenezes / delay.js
Created March 19, 2020 02:05
A delay/sleep function for javascript
function delay(time) {
return new Promise(resolve =>
setTimeout(resolve, time)
)
}
(async () => {
await delay(1000)
console.log('It takes 1 second to be executed.')
@daltonmenezes
daltonmenezes / sorting.js
Last active March 30, 2020 01:56
Sorting numbers by ASC and DESC in javascript without mutating the original array
const numbers = [4, 2, 5, 1, 3]
const orderBy = ([...arr], type = 'ASC') => {
const order = {
ASC: (a, b) => a - b,
DESC: (a, b) => b - a
}
return arr.sort(order[type])
}
@daltonmenezes
daltonmenezes / github-status.js
Created March 31, 2020 04:00
Scraping GitHub status servers with javascript
fetch('https://www.githubstatus.com/')
.then(res => res.text())
.then(text => {
const parser = new DOMParser()
const dom = parser.parseFromString(text, 'text/html')
const status = Array.from(
dom.querySelectorAll("div.component-inner-container.showcased span.component-status"),
el => {
const alias = {
'status-green': 'normal',