Skip to content

Instantly share code, notes, and snippets.

Avatar

Vidar Eldøy eldoy

View GitHub Profile
@eldoy
eldoy / underscore-1.3.3-templates.js
Created Nov 11, 2020 — forked from marlun78/underscore-1.3.3-templates.js
Underscore.js templates as a standalone implementation
View underscore-1.3.3-templates.js
/*!
Underscore.js templates as a standalone implementation.
JavaScript micro-templating, similar to John Resig's implementation.
Underscore templates documentation: http://documentcloud.github.com/underscore/#template
Modifyed by marlun78
*/
(function () {
'use strict';
View javascript_media_query_adapt.js
function adaptFooter(x) {
var nav = document.querySelector('.navigation.mobile_block')
if (x.matches) {
var links = document.querySelectorAll('#footer li')
console.log(links)
var list = document.createElement('ul')
list.setAttribute('id', 'mobile-footer')
links.forEach(function(li) {
list.appendChild(li.cloneNode(true))
})
View replace_in_file_recursively_javascript.js
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const mode = process.argv[2]
const exec = require('child_process').execSync
const root = process.cwd()
const matches = [ "require('presang')", "require('../../index.js')" ]
function replaceIn (from, to) {
@eldoy
eldoy / get-object-value-from-path.js
Created Nov 26, 2019
Get an object value from path and return undefined if not found
View get-object-value-from-path.js
// Get an object value from path
function getValue(obj, path) {
var keys = path.split('.')
for (var i = 0; i < keys.length; i++) {
try {
obj = obj[keys[i]]
} catch(e) {
return
}
@eldoy
eldoy / class_autobind_utils.js
Last active Jul 4, 2018
Automatically bind all function to itself from constructor of a class
View class_autobind_utils.js
// Usage: inside the constructor of a class do util.autobind(this)
const util = {}
// Binds the object instance functions to itself
util.autobind = (obj) => {
const prototype = Object.getPrototypeOf(obj)
const props = Object.getOwnPropertyNames(prototype)
for (const name of props) {
const value = prototype[name]
@eldoy
eldoy / graph_traversal_javascript_with_update.js
Created Apr 29, 2018
Graph traversal javascript with update convert date
View graph_traversal_javascript_with_update.js
// The object graph
let date = new Date()
let data = {
result: {
id: 'query1',
list: [1, 'hello', date],
hash: {hello: 1, bye: 2, z: 'hello', d: date},
goal: 50,
created_at: date
}
@eldoy
eldoy / traverse_object_graph_javascript.js
Created Apr 29, 2018
Traverse object graph pure vanilla javascript
View traverse_object_graph_javascript.js
const traverse = (obj) => {
for (let k in obj) {
if (obj[k] && typeof obj[k] === 'object') {
traverse(obj[k])
} else {
// Do something with obj[k]
}
}
}
View NodeCryptoEncodeDecode.js
// Nodejs encryption with CTR
const crypto = require('crypto')
class Crypto {
constructor () {
this.algorithm = 'aes192'
this.secret = 'd6F3Efeq'
}
encrypt (text) {
View susana-rethinkdb-connect.rb
# Add rethinkdb to Gemfile
# Add to boot.rb: autoload :RethinkDB, 'rethinkdb'
# Set up RabbitMQ queue as well
# Load database settings
config = (YAML.load_file('./config/database.yml') || {})[App.env]
# Connect database
if config