Skip to content

Instantly share code, notes, and snippets.

View FireNeslo's full-sized avatar

Øystein Ø. Olsen FireNeslo

  • Scout Gaming Group
  • Norge/Norway
View GitHub Profile
@FireNeslo
FireNeslo / web-crypto-jwt.js
Last active March 24, 2024 04:27
JWT tokens using web crypto
const encoder = new TextEncoder()
const decoder = new TextDecoder()
const ALGORITHMS = {
HS: { name: 'HMAC' },
ES: { name: 'ECDSA', namedCurve: 'P-256' },
RS: { name: 'RSASSA-PKCS1-v1_5', modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]) },
PS: { name: 'RSA-PSS', saltLength: 128, modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]) },
}
function schase(models, objects = {}) {
function resolve(data) {
for(const [ name, object ] of Object.entries(data)) {
const [ _, base, ref ] = /(.*?)(_id|Id|Ids|_ids)?$/.exec(name)
const [ plural ] = /es|s$/.exec(ref) || []
const model = base.replace(/es|s$/, '')
if(!objects[model]) objects[model] = {}
self.setImmediate = cb => Promise.resolve().then(cb)
var fsModule = new Blob([`
module.exports = {
readdirSync() {
return []
},
readFileSync(file) {
const xhr = new XMLHttpRequest()
@FireNeslo
FireNeslo / demo.es6
Last active January 2, 2018 01:47
Simple es6 heredoc thingy
var html = heredoc `
<section>
<h1> title</h1>
<p>content</p>
</section>
`
@FireNeslo
FireNeslo / immap.js
Last active May 14, 2017 17:52
Immutable thingy
var {ARRAY, NUMBER, STRING, BOOLEAN, OBJECT} = {
ARRAY: Symbol('array'),
OBJECT: Symbol('object'),
NUMBER: Symbol('number'),
STRING: Symbol('string'),
BOOLEAN: Symbol('boolean'),
}
function flatMap(list, callback) {
const result = []
var structure = (function () {
"use strict";
function BaseStruct(values) {
for(var i= values.length-1; i>= 0; i--) {
this[i] = values[i]
}
}
BaseStruct.prototype.toJSON = function toJSON() {
var array = new Array(this.length)
for(var i = array.length-1; i >= 0; i--) {
@FireNeslo
FireNeslo / Objserver.js
Created July 26, 2013 14:37
Observer like object to maintain a reference to data that can or will change a lot.
/**
Creates a new Objserver.
@class Observes an object and updates its value on changes that are applied.
@param [value] - Optional init value; -
*/
function Objserver(value) {
this.$value = value;
this.$change = [];
}
function jsonPath(obj,string) {
@FireNeslo
FireNeslo / ObjMap
Last active December 20, 2015 01:19
An object with map and array like methods that keeps references to the same object for as long as possible; if you attatch a promise it will also resolve that promise before attempting to run any other chained functions. Useful for keeping references in for example angularjs while executing async logic.
function addScript(src) {
var script = document.createElement("script");
script.src = src;
document.body.appendChild(script);
return addScript;
}
addScript('//rawgithub.com/kriskowal/q/master/q.min.js');
/**
* A Object wrapper with some iteration helpers.
* @constructor
@FireNeslo
FireNeslo / hardly-functional
Last active December 19, 2015 21:58
Weird string to function thing using to-Function
var fn = require('to-function');
function toObject(keys,values) {
  var rv = {};
  for (var i = 0; i < keys.length; ++i)
    if (keys[i] !== undefined) rv[keys[i]] = values[i];
  return rv;
}
function h(f) {
var args = f.split("=>")[0].split(",");
var func = f.split("=>")[1].split('-').map(function(fs){
@FireNeslo
FireNeslo / debug_object_as_string.js
Last active December 16, 2015 21:39
This is just a small function I created to see the structure of an event object in the log i recieved from phonegap. It's great for everyone that hates the "[object Object]" string in their logs.
function stringDebug(sender, method, object) {
var tostring =Object.prototype.toString;
Object.prototype.toString = function(padding) {
padding = padding || 3;
var res = Array(padding-2).join(" ")+"{";
var _self = this;
for(var attr in _self) {
res += '\n' + Array(padding).join(" ") + attr + " : " + ((this[attr] && (padding +2) < 12) ? this[attr].toString(padding +2) : "null");
}
return res+"\n"+Array(padding-2).join(" ")+"}";