Skip to content

Instantly share code, notes, and snippets.

View creationix's full-sized avatar
💚
<3

Tim Caswell creationix

💚
<3
View GitHub Profile
@creationix
creationix / nativescript-file-system-example
Created October 5, 2017 19:36 — forked from mikebranstein/nativescript-file-system-example
NativeScript file-system module example
var fileSystemModule = require("file-system");
var fileName = "persistedFile.json";
var file = fileSystemModule.knownFolders.documents().getFile(fileName);
var data = [{"id": "1", "value": "NativeScript"}, ...];
// write data to the file, converted to a JSON string first
file.writeText(JSON.stringify(data));
// read data from the file
function getKeys(obj) {
all = {};
function get(obj) {
var keys = Object.keys(obj);
for (var i = 0, l= keys.length; i < l; i++) {
var key = keys[i];
all[key] = true;
var value = obj[key];
if (value instanceof Object) get(value);
}
// repo is assumed to be in closure
var repo;
// commit - the hash of the current commit
// path - the path to the hash in the current commit
// callback -> the previous blob value
function findPrevious(commit, path, callback) {
var log; // The log stream
var currentBlob; // The current value of the blob
var oldBlob; // The last value of the blob
@creationix
creationix / benchCallbacks.js
Created April 12, 2012 20:31 — forked from bjouhier/benchCallbacks.js
streamline vs. callbacks bench
"use strict";
var fs = require('fs');
var cache = {}, hit = 0, missed = 0;
function load(name, cb) {
var res = cache[name];
if (res) {
process.nextTick(function() {
hit++;
cb(null, res);
function Person() {
// properties and validations
this.attr(
{ id: Number, unique: true, nullable: false },
{ email: String, unique: true, nullable: false, min: 1, max: 55, format: '[a-b]' },
{ salt: String },
{ pswd: String },
{ active: Boolean, init: false },
{ tags: Array }
var Pattern = Object.create(Object.prototype, {
// Implement extend for easy prototypal inheritance
extend: {value: function extend(obj) {
if (obj === undefined) return Object.create(this);
obj.__proto__ = this;
Object.freeze(obj); // Lock the prototype to enforce no changes
return obj;
}},
@creationix
creationix / newisevil.js
Created August 11, 2010 20:38 — forked from brianleroux/wtftim.js
new is evil!
var Person = function( name ){ this.name = name };
Person.create = function(name) { return new Person(name) };
Brian = new Person('brianleroux');
Tim = Person.create('tim');
// Lame, I would do this one of three ways depending on how it will be used
// If the example really was this simple and there was nothing else involved,
// I would use object literals. No need to overcomplicate things
//usage: $ node wav2mp3.js file1.wav file2.wav
// converts to .mp3 format and deletes original .wav file
var fs = require('fs');
var sys = require('sys');
var Step = require('step');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
function convert(file, callback){
var HTTPParser = process.binding('http_parser').HTTPParser;
var net = require('net');
var path = require('path');
var sys = require('sys');
var Worker = require('webworker/webworker').Worker;
var VHOSTS = ['foo.bar.com', 'baz.bizzle.com'];
var WORKERS = {};
VHOSTS.forEach(function(vh) {