Skip to content

Instantly share code, notes, and snippets.

View romgrk's full-sized avatar
♥️
hey

Rom Grk romgrk

♥️
hey
View GitHub Profile

Working with Arrays in JavaScript

JavaScript has several very useful methods for working with arrays, e.g. .forEach, .map, .filter and .reduce. However, the version of JavaScript in the workflow is too old to have them, you must therefore include a polyfill like this one.

You can take advantage of function hoisting (which means function are usable, even if their definition is after the code) as such:

extendArray() // => This will add the above mentioned methods on Array objects
/*
* SQL
*/
function sqlQuery(query, connectionString) {
var adOpenDynamic = 2;
var adLockOptimistic = 3;
var rs = new ActiveXObject('ADODB.Recordset');
rs.open(query, connectionString, adOpenDynamic, adLockOptimistic);
/*
* Excel
*/
// Read all rows an returns an array of arrays
function readExcelAsRows(path, sheetNumber) {
var xls = new ActiveXObject('Excel.Application');
xls.workbooks.open(path);
var sheet = xls.sheets.item(sheetNumber || 1)

Syntax

ES6 ES5
const fn = (n) => n * n function fn(n) { return n * n }
const fn = (n) => { n += 1; return n } function fn(n) { n += 1; return n }
const fn = (name) => ({ value: name }) function fn(name) { return { value: name }; }
const fn = ({x, y}) => x + y function fn(object) { var x = object.x, y = object.y; return x + y }
const newObject = { value: 1, ...object } var newObject = Object.assign({ value: 1 }, object) Object.assign
const newArray = [...array, 1] var newArray = array.slice(0, array.length).concat(1)
@romgrk
romgrk / file-control.js
Created September 21, 2017 03:31
trigger file download & trigger open file dialog from javascript
function download(filename, text) {
const fileBlob = new Blob([text], { type: 'application/octet-binary' })
const url = URL.createObjectURL(fileBlob)
const link = document.createElement('a')
link.setAttribute('href', url)
link.setAttribute('download', filename)
if (document.createEvent) {
const event = document.createEvent('MouseEvents')
watch -n5 "curl 'https://tickets.centrebell.ca/info/showshop.availWSS/eaf2737b-cd65-4d38-bb7a-615fd488e6c6?resp=json' -H 'Pragma: no-cache' -H 'Origin: https://tickets.evenko.ca' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6,es;q=0.4,la;q=0.2' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.49 Safari/537.36' -H 'Accept: */*' -H 'Referer: https://tickets.evenko.ca/shop/' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed | node -p 'const data = JSON.parse(fs.readFileSync(0).toString())
const seats = Object.values(data.result.eventavail).concat(Object.values(data.result.eventavail_ss))
const flatten = list => list.reduce((acc, cur) => acc.concat(cur), [])
const last = list => list[list.length - 1]
flatten(seats).some(last) && process.exit(0) || process.exit(1)' && notify-send -u critical SEAT 'seat available'"
@romgrk
romgrk / slice-and-merge.js
Created October 16, 2017 21:47
Slices and merges bigWig files into a single bedGraph file
#!/usr/bin/env node
/*
* slice-and-merge - slices and merges bigWig files into a single bedGraph
*
* Usage:
* slice-and-merge [OPTIONS] file1.bw file2.bw .. fileN.bw
*
* Options:
* -o, --output <FILE> output file (default: out.bedGraph)
* -b, --bin <DIR> defines the bin directory for utils (default: ./bin)
'use babel'
/*
* Your init script
*
* Atom will evaluate this file each time a new window is opened. It is run
* after packages are loaded/activated and after the previous editor state
* has been restored.
*
* An example hack to log to the console when each text editor is saved.
*
/*
* server.js
*/
const http = require('http')
const port = process.env.PORT || 3000
--langdef=scss
--langmap=scss:.scss
--regex-scss=/^[ \t]*\.([A-Za-z0-9_-]+[^{]+)/.\1/c,class,classes/
--regex-scss=/^[ \t]*#([A-Za-z0-9_-]+[^{]+)/#\1/i,id,ids/
--regex-scss=/^[ \t]*([A-Za-z0-9_-]+[^{]+)\{/\1/t,tag,tags/
--regex-scss=/^[ \t]*@media\s+([A-Za-z0-9_-]+)/\1/m,media,medias/
--regex-scss=/^[ \t]*\$([A-Za-z0-9_-]+)/\$\1/v,var,variables/