[Pkg-javascript-commits] [node-simple-fmt] 01/03: Imported Upstream version 0.1.0+20130419

Julien Puydt julien.puydt at laposte.net
Fri Oct 16 17:27:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-simple-fmt.

commit a662c2c1ca0abcdda3d66bdc3cf08500f498a96e
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Wed Oct 14 21:45:04 2015 +0200

    Imported Upstream version 0.1.0+20130419
---
 LICENSE                  | 19 +++++++++++++++++
 README.md                | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
 package.json             | 25 +++++++++++++++++++++++
 simple-fmt.js            | 33 ++++++++++++++++++++++++++++++
 test/simple-fmt-tests.js | 49 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 179 insertions(+)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..4b87819
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Olov Lassus <olov.lassus at gmail.com>
+
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7d82a99
--- /dev/null
+++ b/README.md
@@ -0,0 +1,53 @@
+# 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
+<script src="simple-fmt/simple-fmt.js"></script>
+```
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..f461171
--- /dev/null
+++ b/package.json
@@ -0,0 +1,25 @@
+{
+    "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 <olov.lassus at gmail.com>",
+    "license": "MIT"
+}
diff --git a/simple-fmt.js b/simple-fmt.js
new file mode 100644
index 0000000..a278b86
--- /dev/null
+++ b/simple-fmt.js
@@ -0,0 +1,33 @@
+// simple-fmt.js
+// MIT licensed, see LICENSE file
+// Copyright (c) 2013 Olov Lassus <olov.lassus at gmail.com>
+
+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;
+}
diff --git a/test/simple-fmt-tests.js b/test/simple-fmt-tests.js
new file mode 100644
index 0000000..972f903
--- /dev/null
+++ b/test/simple-fmt-tests.js
@@ -0,0 +1,49 @@
+"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();
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-simple-fmt.git



More information about the Pkg-javascript-commits mailing list