UNPKG

yapm

Version:

package manager for io.js (npm fork)

78 lines (77 loc) 6.46 kB
{ "name": "printf", "version": "0.1.2", "description": "Full implementation of the `printf` family in pure JS.", "keywords": [ "printf", "formatting", "sprintf", "format", "output", "print" ], "homepage": "http://www.adaltas.com/projects/node-printf", "author": { "name": "David Worms", "email": "david@adaltas.com" }, "maintainers": [ { "name": "David Worms", "email": "david@adaltas.com" } ], "contributors": [ { "name": "David Worms", "email": "david@adaltas.com" }, { "name": "Aluísio Augusto Silva Gonçalves", "email": "aluisio@aasg.name" }, { "name": "Xavier Mendez", "email": "jmendeth@gmail.com" }, { "name": "LLeo", "email": "lleoem@gmail.com" } ], "main": "./lib/printf", "engines": { "node": ">= 0.1.90" }, "directories": { "lib": "./lib", "test": "./test", "doc": "./doc" }, "scripts": { "test": "make test" }, "devDependencies": { "coffee-script": "latest", "should": "latest", "mocha": "latest", "semver": "latest" }, "repository": { "type": "git", "url": "https://github.com/wdavidw/node-printf.git" }, "bugs": { "url": "https://github.com/wdavidw/node-printf/issues" }, "licenses": [ { "type": "BSD", "url": "http://opensource.org/licenses/BSD-3-Clause" } ], "readme": "[![Build Status](https://secure.travis-ci.org/wdavidw/node-printf.png)](http://travis-ci.org/wdavidw/node-printf)\n\n<pre>\n _ _ __ \n (_) | | / _|\n _ __ _ __ _ _ __ | |_| |_ \n | '_ \\| '__| | '_ \\| __| _|\n | |_) | | | | | | | |_| | \n | .__/|_| |_|_| |_|\\__|_| \n | | \n |_| \n\n</pre>\n\nA complete implementation of the **`printf` C functions family**\nfor [Node.JS][node], written in pure JavaScript. \nThe code is strongly inspired by the one available in the [Dojo Toolkit][dojo].\n\n**Bonus!** You get extra features, like the `%O` converter (which `inspect`s\nthe argument). See [Extra Features](#extra-features) below.\n\nInstalling\n----------\n\nVia [NPM][npm]:\n\n``` bash\n$ npm install printf\n```\n\nUsage\n-----\n\nUse it like you would in C (`printf`/`sprintf`):\n\n``` javascript\nvar printf = require('printf');\nvar result = printf(format, args...);\n```\n\nIt can also output the result for you, as `fprintf`:\n\n``` javascript\nvar printf = require('printf');\nprintf(write_stream, format, args...);\n```\n\nFeatures\n--------\n \n### Flags\n\n##### ` ` (space)\n\n``` javascript\nassert.eql(' -42', printf('% 5d', -42));\n```\n\n##### `+` (plus)\n\n``` javascript\nassert.eql(' +42', printf('%+5d', 42));\n```\n\n##### `0` (zero)\n\n``` javascript\nassert.eql('00042', printf('%05d', 42));\n```\n\n##### `-` (minus)\n\n``` javascript\nassert.eql('42 ', printf('%-5d', 42));\n```\n\n### Width / precision\n\n``` javascript\nassert.eql('42.90', printf('%.2f', 42.8952));\nassert.eql('042.90', printf('%06.2f', 42.8952));\n```\n\n### Numerical bases\n\n``` javascript\nassert.eql('\\x7f', printf('%c', 0x7f));\nassert.eql('a', printf('%c', 'a'));\nassert.eql('\"', printf('%c', 34));\n```\n\n### Miscellaneous\n\n``` javascript\nassert.eql('10%', printf('%d%%', 10));\nassert.eql('+hello+', printf('+%s+', 'hello'));\nassert.eql('$', printf('%c', 36));\n```\n\nExtra features!\n---------------\n\n### Inspector\n\nThe `%O` converter will call [`util.inspect(...)`][util_inspect] at the argument:\n\n``` javascript\nassert.eql(\"Debug: { hello: 'Node', repeat: false }\",\n printf('Debug: %O', {hello: 'Node', \"repeat\": false})\n);\nassert.eql(\"Test: { hello: 'Node' }\",\n printf('%2$s: %1$O', {\"hello\": 'Node'}, 'Test')\n);\n```\n\n**Important:** it's a capital \"O\", *not* a zero!\n\nSpecifying a precision lets you control the depth up to which the object is formatted:\n\n``` javascript\nassert.eql(\"Debug: { depth0: { depth1_: 0, depth1: [Object] } }\",\n printf('Debug: %.1O', {depth0: {depth1: {depth2: {depth3: true}}, depth1_: 0}})\n);\n```\n\nYou can also use `%A` instead of `%O` to disable representation of non-enumerable properties:\n\n``` javascript\nassert.eql(\"With non-enumerable properties: [ 1, 2, 3, 4, 5, [length]: 5 ]\",\n printf('With non-enumerable properties: %O', [1, 2, 3, 4, 5])\n);\nassert.eql(\"Without non-enumerable properties: [ 1, 2, 3, 4, 5 ]\",\n printf('Without non-enumerable properties: %A', [1, 2, 3, 4, 5])\n);\n```\n\n### Argument mapping\n\nIn addition to the old-fashioned `n$`, \nyou can use **hashes** and **property names**!\n\n``` javascript\nassert.eql('Hot Pockets',\n printf('%(temperature)s %(crevace)ss', {\n temperature: 'Hot',\n crevace: 'Pocket'\n })\n);\nassert.eql('Hot Pockets',\n printf('%2$s %1$ss', 'Pocket', 'Hot')\n);\n```\n\n### Positionals\n\nLenght and precision can now be variable:\n\n``` javascript\nassert.eql(' foo', printf('%*s', 'foo', 4));\nassert.eql(' 3.14', printf('%*.*f', 3.14159265, 10, 2));\nassert.eql('000003.142', printf('%0*.*f', 3.14159265, 10, 3));\nassert.eql('3.1416 ', printf('%-*.*f', 3.14159265, 10, 4));\n```\n\nDevelopment\n-----------\n\nTests are executed with [Mocha][mocha]. To install it, simple run `npm install`, it will install\nMocha and its dependencies in your project's `node_modules` directory.\n\nTo run the tests:\n\n```bash\nnpm test\n```\n\nTo generate the JavaScript files:\n\n```bash\nmake build\n```\n\nThe test suite is run online with [Travis][travis] against Node.js version 0.6, 0.7, 0.8 and 0.9.\n\nContributors\n------------\n\n* David Worms <https://github.com/wdavidw>\n* Aluísio Augusto Silva Gonçalves <https://github.com/AluisioASG>\n* Xavier Mendez <https://github.com/jmendeth>\n* LLeo <https://github.com/lleo>\n\n\n[dojo]: http://www.dojotoolkit.org \"The Dojo Toolkit\"\n[node]: http://nodejs.org \"The Node.JS platform\"\n[npm]: https://github.com/isaacs/npm \"The Node Package Manager\"\n[util_inspect]: http://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors \"util.inspect() documentation\"\n[expresso]: http://visionmedia.github.com/expresso \"The Expresso TDD\"\n[travis]: https://travisci.org \"Continuous Integration system\"\n[mocha]: http://visionmedia.github.io/mocha \"The Mocha test framework\"\n", "readmeFilename": "README.md", "_id": "printf@0.1.2", "_from": "printf@" }