update
I've created a little repository that simply exposes the final utility as npm module.
It's called html-escaper
there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.
| function hereDoc(f) { | |
| return f.toString(). | |
| replace(/^[^\/]+\/\*!?/, ''). | |
| replace(/\*\/[^\/]+$/, ''); | |
| } | |
| // usage | |
| var tennysonQuote = hereDoc(function() {/*! | |
| Theirs not to make reply, |
| /** | |
| * Allow other processes to execute while iterating over | |
| * an array. Useful for large arrays, or long-running processing | |
| * | |
| * @param {Function} fn iterator fed each element of the array. | |
| * @param {Function} next executed when done | |
| */ | |
| Array.prototype.nonBlockingForEach = function(fn, next) { | |
| var arr = this; | |
| var i = 0; |
| /** | |
| * Outputs a new function with interpolated object property values. | |
| * Use like so: | |
| * var fn = makeInterpolator('some/url/{param1}/{param2}'); | |
| * fn({ param1: 123, param2: 456 }); // => 'some/url/123/456' | |
| */ | |
| var makeInterpolator = (function() { | |
| var rc = { | |
| '\n': '\\n', '\"': '\\\"', | |
| '\u2028': '\\u2028', '\u2029': '\\u2029' |
| (function(scope) { | |
| function destroyAutoGlobals(options) { | |
| var allElements = document.getElementsByTagName("*"), elementId; | |
| for (var i=allElements.length; i--; ) { | |
| elementId = allElements[i].id; | |
| if (elementId && window[elementId] instanceof HTMLElement) { | |
| options && options.verbose && console.log('Destroying window["' + elementId + '"]'); | |
| window[elementId] = null; |
| class UserController < ApplicationController | |
| def create | |
| @user = User.create(UserInput.new(params).create) | |
| end | |
| def update | |
| @user = User.find(params[:id].to_i) | |
| @user.update_attributes(UserInput.new(params).update) | |
| end | |
| function toJSON(node) { | |
| let propFix = { for: 'htmlFor', class: 'className' }; | |
| let specialGetters = { | |
| style: (node) => node.style.cssText, | |
| }; | |
| let attrDefaultValues = { style: '' }; | |
| let obj = { | |
| nodeType: node.nodeType, | |
| }; | |
| if (node.tagName) { |
| var Promise = global.Promise || require('lie'); | |
| function noop(){} | |
| function typeIsObject(x) { | |
| return (typeof x === 'object' && x !== null) || typeof x === 'function'; | |
| } | |
| function ExclusiveStreamReader () { | |
| throw new Error('not implimented'); | |
| } | |
| function ReadableStream(opts) { | |
| var start = opts.start || noop; |
update
I've created a little repository that simply exposes the final utility as npm module.
It's called html-escaper
there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.
| var path = require('path'); | |
| var fs = require('fs'); | |
| var cheerio = require('cheerio'); | |
| var createTemplate = function(id, markup) { | |
| var $ = cheerio.load('<script type="text/ng-template"></script>'); | |
| $('script').attr('id', id).html(markup).html(); | |
| return $.html(); | |
| }; |
| // Most components are defined fully by their render function, | |
| // and all they need to access is the props | |
| var myComponent = createComponent(function (props) { | |
| return React.DOM.h1({}, "Hello " + props.name); | |
| }); | |
| // ...which can be done very succinctly with ES6: | |
| const {h1, div} = React.DOM; | |
| const myComponent = createComponent(({name}) => h1({}, `Hello ${name}`)); |