Skip to content

Instantly share code, notes, and snippets.

View dotproto's full-sized avatar

Simeon Vincent dotproto

View GitHub Profile
@dotproto
dotproto / prototypes_tests.js
Last active February 14, 2024 01:54
Object prototype tests
// http://ejohn.org/blog/objectgetprototypeof/
function instanceOf(object, constructor) {
while (object != null) {
if (object == constructor.prototype) {
return true;
}
object = Object.getPrototypeOf(object);
}
return false;
}
@dotproto
dotproto / inheritance.js
Last active February 14, 2024 01:53
JS Inheritance (ES5)
// == PARENT ===================================================================
// CONSTRUCTOR
// For this demo we'll be inheriting from the Parent class
function Parent() {}
// METHODS
Parent.prototype.foo = function foo() {
return 'FOO';
}
@dotproto
dotproto / tts.js
Last active February 14, 2024 01:53
ES2015 Text to Speech bookmarklet (Ctrl+S). Set up: Copy and paste the contents of this gist into a new bookmark. Use: Select some text and click the bookmarklet to start speaking. Once activated on a page, you can use Ctrl+S to speak the selected text. Speech Synthesis API Info: https://developers.google.com/web/updates/2014/01/Web-apps-that-ta…
javascript: {
/* Adjust voice speed. Default = 1 */
var speed = 2.5;
if (window.runTTS === undefined) {
/* Text to Speech function. Adjust the value of msg.rate to increase/decrease the playback speed. */
window.runTTS = () => {
const text = window.getSelection().toString();
let sourceObject = { foo: { bar: { baz: { qux: { value: "Hit!" } } } } };
sourceObject.foo[Symbol.for('symbol')] = 'symbol value';
function lookup(object, path) {
path = Array.isArray(path) ? path : path.split('.');
for(let i = 0; i < path.length; i++) {
object = object[path[i]];
if (!object) break;
@dotproto
dotproto / ng-util.js
Last active February 14, 2024 01:53
Utilities for working with and debugging Angular.js 1.x apps (ES6)
(function exposeNgUtil(target = window) {
if (!angular) {
console.error(`ERROR: ng-util cannot initialize because "angular" was not found!`);
return null;
}
getScope = el => angular.element( (el || document.getElementsByClassName('ng-scope')[0]) )
let ngUtils = {
// Use Angular.js' DI system to retrieve a provider.
@dotproto
dotproto / test.js
Created August 11, 2016 21:58
Exploring Proxies & [Symbol.toPrimitive]
// http://www.ecma-international.org/ecma-262/6.0/#sec-symbol.toprimitive
let base = {
[Symbol.toPrimitive] : hint => {
console.log(`toPrimitive hint: ${hint}`)
return 1;
},
valueOf: () => 2,
toString: () => '3',
};
@dotproto
dotproto / proxy_test.js
Last active February 14, 2024 01:53
Some test code generated while trying to wrap my head around how JS proxies work and how I might leverage them
// Resources
//
// - http://exploringjs.com/es6/ch_proxies.html
// - https://github.com/tvcutsem/harmony-reflect/issues/41
// - https://esdiscuss.org/topic/proxies-stratifying-tostring-valueof
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive
// - https://console.spec.whatwg.org/#inspection
// NOTE: When Chrome logs "Symbol(Symbol.toPrimitive)", that corisponds to
@dotproto
dotproto / functional_helpers.js
Last active February 14, 2024 01:52
Helper functions I didn't realize I wanted until I wrote them a few times in several different projects. Uses ES6 module syntax.
// ====================
// = OBJECT HELPERS =
// ====================
export const set = (obj, prop, val) => {
obj[prop] = val
return obj
}
export const extractObj = (obj, prop) =>
Object.keys(obj).map(key =>
@dotproto
dotproto / demo.js
Created March 15, 2017 16:27
Observable -> BehaviorSubject
// Open http://reactivex.io/rxjs/manual/overview.html to run this code
var timer = new Rx.Observable((obs) => {
var counter = 0;
var int = setInterval(() => obs.next(++counter), 1000);
return () => {
clearInterval(int);
obs.complete();
}
});
@dotproto
dotproto / linked-list.js
Last active February 14, 2024 01:52
Linked List implementation that behaves like a stack (last in, first out)
const VOID = Symbol('VOID');
class LinkedItem {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {