Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 10, 2011 16:04
Show Gist options
  • Select an option

  • Save jed/964762 to your computer and use it in GitHub Desktop.

Select an option

Save jed/964762 to your computer and use it in GitHub Desktop.

Revisions

  1. jed revised this gist May 31, 2011. 1 changed file with 13 additions and 20 deletions.
    33 changes: 13 additions & 20 deletions LICENSE.txt
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,13 @@
    Copyright (c) 2011 Jed Schmidt, http://jed.is

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    Version 2, December 2004

    Copyright (C) 2011 Jed Schmidt <http://jed.is>

    Everyone is permitted to copy and distribute verbatim or modified
    copies of this license document, and changing it is allowed as long
    as the name is changed.

    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

    0. You just DO WHAT THE FUCK YOU WANT TO.
  2. jed revised this gist May 16, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ function(
    ){
    return function(
    c, // the object called as `this` in the template
    d // the default `with` context of the template (optional)
    d // the `with` context of this template call (optional)
    ){
    return a.replace(
    /#{([^}]*)}/g, // a regexp that finds the interpolated code: "#{<code>}"
    @@ -16,9 +16,9 @@ function(
    "x",
    "with(x)return " + e // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    d // pass the most...
    || b // specific...
    c, // pass the data object as `this`, with
    d // the most
    || b // specific
    || {} // context.
    )
    }
  3. jed revised this gist May 15, 2011. 1 changed file with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions test_kanji.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // This test renders a table with every CJK ideograph.
    // On my MacBook Air, it takes about 3.5 seconds.

    !function() {
    var t = function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}}

    var kanji = []

    for (var i = 0x9faf; i >= 0x4e00; i--) {
    kanji[i] = String.fromCharCode(i)
    }

    var d = new Date

    var row = t( "\
    <tr>\
    <td>#{this}</td>\
    <td>\\u#{this.charCodeAt(0).toString(16)}</td>\
    </tr>"
    )

    var table = t( "\
    <h1>CJK unified ideographs</h1>\
    <table>\
    <tr>\
    <td><b>Glyph</b></td>\
    <td><b>Codepoint</b></td>\
    </tr>\
    #{this.map(row).join('')}\
    </table>",
    {row:row}
    )

    console.log( "compiling templates..." )
    console.log( "compiled templates in " + (new Date - d) + "ms" )

    d = new Date

    console.log( "rendering table..." )

    var html = table(kanji)

    console.log( "rendered table in " + (new Date - d) + "ms" )

    console.log( "here's a sample:\n\n" + html.substr(0, 1000) )
    }()
  4. jed revised this gist May 15, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,8 @@ function(
    e // the code matched by the interpolation
    ){
    return Function(
    "b",
    "with(b)return " + e // the result of the interpolated code
    "x",
    "with(x)return " + e // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    d // pass the most...
  5. jed revised this gist May 15, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,10 @@ function(
    "b",
    "with(b)return " + e // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    d || b || {} // the most specific `with` context
    c, // the object passed to the template as `this`
    d // pass the most...
    || b // specific...
    || {} // context.
    )
    }
    )
  6. jed revised this gist May 15, 2011. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    !function() {
    // Put the template in your code...
    var t = function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}}

    // and make a template, using `#{<code>}` for JavaScript.
    var hello = t("Hello, #{this.name || 'world'}!")

    // Run the template with an object...
    console.log( // => "Hello, Jed!"
    hello({name: "Jed"})
    )

    // or without.
    console.log( // => "Hello, world!"
    hello()
    )

    // Or make a template with a context...
    var greet = t(
    "Allow me to say, '#{hello(this)}'",
    {hello: hello}
    )

    // and run it as is...
    console.log( // => "Allow me to say, 'Hello, Jed!'"
    greet({name: "Jed"})
    )

    // or with its own context.
    console.log( // => "Allow me to say, 'Hello, JED!'"
    greet(
    {name: "Jed"},
    {hello: function(x) {
    return hello({ name: x.name.toUpperCase() })
    }}
    )
    )
    }()
  7. jed revised this gist May 15, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion annotated.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function(
    "b",
    "with(b)return " + e // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    c, // the object passed to the template as `this`
    d || b || {} // the most specific `with` context
    )
    }
  8. jed revised this gist May 15, 2011. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion annotated.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ function(
    "with(b)return " + e // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    d || b || c // the most specific `with` context
    d || b || {} // the most specific `with` context
    )
    }
    )
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||c)})}}
    function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}}
  9. jed revised this gist May 15, 2011. 2 changed files with 7 additions and 6 deletions.
    11 changes: 6 additions & 5 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -3,20 +3,21 @@ function(
    b // the default `with` context of the template (optional)
    ){
    return function(
    c // the object called as `this` in the template
    c, // the object called as `this` in the template
    d // the default `with` context of the template (optional)
    ){
    return a.replace(
    /#{([^}]*)}/g, // a regexp that finds the interpolated code: "#{<code>}"
    function(
    a, // not used, only positional
    d // the code matched by the interpolation
    e // the code matched by the interpolation
    ){
    return Function(
    "b",
    "with(b)return " + d // the result of the interpolated code
    "with(b)return " + e // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    b || c // the `with` context if provided, or the passed object
    c, // the object passed to the template as `this`
    d || b || c // the most specific `with` context
    )
    }
    )
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return function(c){return a.replace(/#{([^}]*)}/g,function(a,d){return Function("b","with(b)return "+d).call(c,b||c)})}}
    function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||c)})}}
  10. jed revised this gist May 15, 2011. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions LICENSE.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    Copyright (c) 2011 Jed Schmidt, http://jed.is

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11. jed revised this gist May 15, 2011. 2 changed files with 25 additions and 1 deletion.
    24 changes: 24 additions & 0 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function(
    a, // the string source from which the template is compiled
    b // the default `with` context of the template (optional)
    ){
    return function(
    c // the object called as `this` in the template
    ){
    return a.replace(
    /#{([^}]*)}/g, // a regexp that finds the interpolated code: "#{<code>}"
    function(
    a, // not used, only positional
    d // the code matched by the interpolation
    ){
    return Function(
    "b",
    "with(b)return " + d // the result of the interpolated code
    ).call(
    c, // the object passed to the template as `this`
    b || c // the `with` context if provided, or the passed object
    )
    }
    )
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function t(s,n){return t[n]=function(d){return s.replace(/{{([^}]+)}}/g,function(s,c){return Function("t","with(t)return "+c).call(d,t)})}}
    function(a,b){return function(c){return a.replace(/#{([^}]*)}/g,function(a,d){return Function("b","with(b)return "+d).call(c,b||c)})}}
  12. jed revised this gist May 13, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    {
    "name": "template",
    "keywords": ["template", "HTML", "render"]
    }
  13. jed revised this gist May 10, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(s,d){return s.replace(/{([^}]+)}/g,function(s,c){with(d)return eval(c)})}
    function t(s,n){return t[n]=function(d){return s.replace(/{{([^}]+)}}/g,function(s,c){return Function("t","with(t)return "+c).call(d,t)})}}
  14. jed revised this gist May 10, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(){/******************************************************************************************************************************/}
    function(s,d){return s.replace(/{([^}]+)}/g,function(s,c){with(d)return eval(c)})}
  15. 140bytesbot created this gist May 9, 2011.
    1 change: 1 addition & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    function(){/******************************************************************************************************************************/}