pax_global_header00006660000000000000000000000064121342331020014501gustar00rootroot0000000000000052 comment=e0c624b4be8404ed06dca42e31e363e989820e0e simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/000077500000000000000000000000001213423310200205525ustar00rootroot00000000000000simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/LICENSE000066400000000000000000000020671213423310200215640ustar00rootroot00000000000000Copyright (c) 2013 Olov Lassus 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. simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/README.md000066400000000000000000000020471213423310200220340ustar00rootroot00000000000000# simple-fmt.js A maximally minimal string formatting library. Use it to make your code more readable compared to plain old string concatenation using `+`. The code is shorter than the MIT license text so it doesn't hog you down and you can use it everywhere. Works in node and browsers. ## Usage ```javascript var fmt = require("simple-fmt"); console.log(fmt("hello {0} of age {1}", name, age)); ``` instead of ```javascript console.log("hello " + name + " of age " + age); ``` because string formatting with `+` makes your eyes bleed and fingers hurt. There's also `fmt.obj(string, obj)` and `fmt.repeat(string, n)`: ```javascript var o = {name: "xyz", age: 42}; fmt.obj("hello {name} of age {age}", obj); fmt.repeat("*", 3); // "***" ``` That's it. ## Installation ### Node Install using npm npm install simple-fmt ```javascript var fmt = require("simple-fmt"); ``` ### Browser Clone the repo and include it in a script tag git clone https://github.com/olov/simple-fmt.git ```html ``` simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/package.json000066400000000000000000000010601213423310200230350ustar00rootroot00000000000000{ "name": "simple-fmt", "version": "0.1.0", "description": "maximally minimal string formatting library", "main": "simple-fmt.js", "repository": { "type": "git", "url": "https://github.com/olov/simple-fmt.git" }, "keywords": [ "fmt", "format", "formatting", "string", "template" ], "scripts": { "test": "tap test/*.js" }, "devDependencies" : { "tap" : "~0.4.0" }, "author": "Olov Lassus ", "license": "MIT" } simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/simple-fmt.js000066400000000000000000000015131213423310200231650ustar00rootroot00000000000000// simple-fmt.js // MIT licensed, see LICENSE file // Copyright (c) 2013 Olov Lassus var fmt = (function() { "use strict"; function fmt(str, var_args) { var args = Array.prototype.slice.call(arguments, 1); return str.replace(/\{(\d+)\}/g, function(s, match) { return (match in args ? args[match] : s); }); } function obj(str, obj) { return str.replace(/\{([_$a-zA-Z0-9][_$a-zA-Z0-9]*)\}/g, function(s, match) { return (match in obj ? obj[match] : s); }); } function repeat(str, n) { return (new Array(n + 1)).join(str); } fmt.fmt = fmt; fmt.obj = obj; fmt.repeat = repeat; return fmt; })(); if (typeof module !== "undefined" && typeof module.exports !== "undefined") { module.exports = fmt; } simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/test/000077500000000000000000000000001213423310200215315ustar00rootroot00000000000000simple-fmt-e0c624b4be8404ed06dca42e31e363e989820e0e/test/simple-fmt-tests.js000066400000000000000000000023431213423310200253060ustar00rootroot00000000000000"use strict"; var test = require("tap").test; var fmt = require("../"); test("fmt", function(t) { t.equals(fmt("all your {0} are belong to {1}", "base", "us"), "all your base are belong to us"); var obj = { toString: function() { return "yoyoma"; }, }; t.equals(fmt("object is called {0} and is {1} ms old", obj, 1), "object is called yoyoma and is 1 ms old"); t.equals(fmt("no arguments => no modifs {0} {1}"), "no arguments => no modifs {0} {1}"); t.end(); }); test("fmt.obj", function(t) { var obj2 = { name: "yoyoma", age: 1, }; t.equals(fmt.obj("object is called {name} and is {age} ms old", obj2), "object is called yoyoma and is 1 ms old"); t.equals(fmt.obj("no matching properties => no modifs {0} {1} {name} {age}", {}), "no matching properties => no modifs {0} {1} {name} {age}"); t.equals(fmt.obj("works for arrays too: [{2}, {1}, {0}]", ["one", "two", "three"]), "works for arrays too: [three, two, one]"); t.end(); }); test("fmt.repeat", function(t) { t.equals(fmt.repeat("*", 3), "***"); t.equals(fmt.repeat("*", 0), ""); t.equals(fmt.repeat("", 3), ""); t.end(); });