Skip to content

Instantly share code, notes, and snippets.

@petermikitsh
Created December 28, 2017 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petermikitsh/e654754e55f867112fce6ce5624baad0 to your computer and use it in GitHub Desktop.
Save petermikitsh/e654754e55f867112fce6ce5624baad0 to your computer and use it in GitHub Desktop.
requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var timm = require('timm');
var obj = {foo: ''};
var newObj = timm.setIn(obj, ['foo', 'bar'], 'baz');
console.log('the object is', newObj);
setTimeout(function(){require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var process=module.exports={};var cachedSetTimeout;var cachedClearTimeout;function defaultSetTimout(){throw new Error("setTimeout has not been defined")}function defaultClearTimeout(){throw new Error("clearTimeout has not been defined")}(function(){try{if(typeof setTimeout==="function"){cachedSetTimeout=setTimeout}else{cachedSetTimeout=defaultSetTimout}}catch(e){cachedSetTimeout=defaultSetTimout}try{if(typeof clearTimeout==="function"){cachedClearTimeout=clearTimeout}else{cachedClearTimeout=defaultClearTimeout}}catch(e){cachedClearTimeout=defaultClearTimeout}})();function runTimeout(fun){if(cachedSetTimeout===setTimeout){return setTimeout(fun,0)}if((cachedSetTimeout===defaultSetTimout||!cachedSetTimeout)&&setTimeout){cachedSetTimeout=setTimeout;return setTimeout(fun,0)}try{return cachedSetTimeout(fun,0)}catch(e){try{return cachedSetTimeout.call(null,fun,0)}catch(e){return cachedSetTimeout.call(this,fun,0)}}}function runClearTimeout(marker){if(cachedClearTimeout===clearTimeout){return clearTimeout(marker)}if((cachedClearTimeout===defaultClearTimeout||!cachedClearTimeout)&&clearTimeout){cachedClearTimeout=clearTimeout;return clearTimeout(marker)}try{return cachedClearTimeout(marker)}catch(e){try{return cachedClearTimeout.call(null,marker)}catch(e){return cachedClearTimeout.call(this,marker)}}}var queue=[];var draining=false;var currentQueue;var queueIndex=-1;function cleanUpNextTick(){if(!draining||!currentQueue){return}draining=false;if(currentQueue.length){queue=currentQueue.concat(queue)}else{queueIndex=-1}if(queue.length){drainQueue()}}function drainQueue(){if(draining){return}var timeout=runTimeout(cleanUpNextTick);draining=true;var len=queue.length;while(len){currentQueue=queue;queue=[];while(++queueIndex<len){if(currentQueue){currentQueue[queueIndex].run()}}queueIndex=-1;len=queue.length}currentQueue=null;draining=false;runClearTimeout(timeout)}process.nextTick=function(fun){var args=new Array(arguments.length-1);if(arguments.length>1){for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i]}}queue.push(new Item(fun,args));if(queue.length===1&&!draining){runTimeout(drainQueue)}};function Item(fun,array){this.fun=fun;this.array=array}Item.prototype.run=function(){this.fun.apply(null,this.array)};process.title="browser";process.browser=true;process.env={};process.argv=[];process.version="";process.versions={};function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")};process.umask=function(){return 0}},{}],timm:[function(require,module,exports){(function(process){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};exports.clone=clone;exports.addLast=addLast;exports.addFirst=addFirst;exports.removeLast=removeLast;exports.removeFirst=removeFirst;exports.insert=insert;exports.removeAt=removeAt;exports.replaceAt=replaceAt;exports.getIn=getIn;exports.set=set;exports.setIn=setIn;exports.update=update;exports.updateIn=updateIn;exports.merge=merge;exports.mergeIn=mergeIn;exports.omit=omit;exports.addDefaults=addDefaults;var INVALID_ARGS="INVALID_ARGS";function throwStr(msg){throw new Error(msg)}var hasOwnProperty={}.hasOwnProperty;function clone(obj){if(Array.isArray(obj))return obj.slice();var keys=Object.keys(obj);var out={};for(var i=0;i<keys.length;i++){var key=keys[i];out[key]=obj[key]}return out}function doMerge(fAddDefaults,first){var out=first;!(out!=null)&&throwStr(process.env.NODE_ENV!=="production"?"At least one object should be provided to merge()":INVALID_ARGS);var fChanged=false;for(var _len=arguments.length,rest=Array(_len>2?_len-2:0),_key=2;_key<_len;_key++){rest[_key-2]=arguments[_key]}for(var idx=0;idx<rest.length;idx++){var obj=rest[idx];if(obj==null)continue;var keys=Object.keys(obj);if(!keys.length)continue;for(var j=0;j<=keys.length;j++){var key=keys[j];if(fAddDefaults&&out[key]!==undefined)continue;var nextVal=obj[key];if(nextVal===undefined||nextVal===out[key])continue;if(!fChanged){fChanged=true;out=clone(out)}out[key]=nextVal}}return out}function isObject(o){var type=typeof o==="undefined"?"undefined":_typeof(o);return o!=null&&(type==="object"||type==="function")}function addLast(array,val){if(Array.isArray(val))return array.concat(val);return array.concat([val])}function addFirst(array,val){if(Array.isArray(val))return val.concat(array);return[val].concat(array)}function removeLast(array){if(!array.length)return array;return array.slice(0,array.length-1)}function removeFirst(array){if(!array.length)return array;return array.slice(1)}function insert(array,idx,val){return array.slice(0,idx).concat(Array.isArray(val)?val:[val]).concat(array.slice(idx))}function removeAt(array,idx){if(idx>=array.length||idx<0)return array;return array.slice(0,idx).concat(array.slice(idx+1))}function replaceAt(array,idx,newItem){if(array[idx]===newItem)return array;var len=array.length;var result=Array(len);for(var i=0;i<len;i++){result[i]=array[i]}result[idx]=newItem;return result}function getIn(obj,path){!Array.isArray(path)&&throwStr(process.env.NODE_ENV!=="production"?"A path array should be provided when calling getIn()":INVALID_ARGS);if(obj==null)return undefined;var ptr=obj;for(var i=0;i<path.length;i++){var key=path[i];ptr=ptr!=null?ptr[key]:undefined;if(ptr===undefined)return ptr}return ptr}function set(obj,key,val){var fallback=typeof key==="number"?[]:{};var finalObj=obj==null?fallback:obj;if(finalObj[key]===val)return finalObj;var obj2=clone(finalObj);obj2[key]=val;return obj2}function doSetIn(obj,path,val,idx){var newValue=void 0;var key=path[idx];if(idx===path.length-1){newValue=val}else{var nestedObj=isObject(obj)?obj[key]:typeof path[idx+1]==="number"?[]:{};newValue=doSetIn(nestedObj,path,val,idx+1)}return set(obj,key,newValue)}function setIn(obj,path,val){if(!path.length)return val;return doSetIn(obj,path,val,0)}function update(obj,key,fnUpdate){var prevVal=obj==null?undefined:obj[key];var nextVal=fnUpdate(prevVal);return set(obj,key,nextVal)}function updateIn(obj,path,fnUpdate){var prevVal=getIn(obj,path);var nextVal=fnUpdate(prevVal);return setIn(obj,path,nextVal)}function merge(a,b,c,d,e,f){for(var _len2=arguments.length,rest=Array(_len2>6?_len2-6:0),_key2=6;_key2<_len2;_key2++){rest[_key2-6]=arguments[_key2]}return rest.length?doMerge.call.apply(doMerge,[null,false,a,b,c,d,e,f].concat(rest)):doMerge(false,a,b,c,d,e,f)}function mergeIn(a,path,b,c,d,e,f){var prevVal=getIn(a,path);if(prevVal==null)prevVal={};var nextVal=void 0;for(var _len3=arguments.length,rest=Array(_len3>7?_len3-7:0),_key3=7;_key3<_len3;_key3++){rest[_key3-7]=arguments[_key3]}if(rest.length){nextVal=doMerge.call.apply(doMerge,[null,false,prevVal,b,c,d,e,f].concat(rest))}else{nextVal=doMerge(false,prevVal,b,c,d,e,f)}return setIn(a,path,nextVal)}function omit(obj,attrs){var omitList=Array.isArray(attrs)?attrs:[attrs];var fDoSomething=false;for(var i=0;i<omitList.length;i++){if(hasOwnProperty.call(obj,omitList[i])){fDoSomething=true;break}}if(!fDoSomething)return obj;var out={};var keys=Object.keys(obj);for(var _i=0;_i<keys.length;_i++){var key=keys[_i];if(omitList.indexOf(key)>=0)continue;out[key]=obj[key]}return out}function addDefaults(a,b,c,d,e,f){for(var _len4=arguments.length,rest=Array(_len4>6?_len4-6:0),_key4=6;_key4<_len4;_key4++){rest[_key4-6]=arguments[_key4]}return rest.length?doMerge.call.apply(doMerge,[null,true,a,b,c,d,e,f].concat(rest)):doMerge(true,a,b,c,d,e,f)}var timm={clone:clone,addLast:addLast,addFirst:addFirst,removeLast:removeLast,removeFirst:removeFirst,insert:insert,removeAt:removeAt,replaceAt:replaceAt,getIn:getIn,set:set,setIn:setIn,updateIn:updateIn,merge:merge,mergeIn:mergeIn,omit:omit,addDefaults:addDefaults};exports.default=timm}).call(this,require("_process"))},{_process:1}]},{},[]);var timm=require("timm");var obj={foo:""};var newObj=timm.setIn(obj,["foo","bar"],"baz");console.log("the object is",newObj)},0);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"timm": "1.3.0"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment