{"name":"utf8-binary-cutter","version":"0.8.0","description":"truncate UTF-8 strings to a given binary size","main":"main.js","scripts":{"test":"mocha tests/ --recursive"},"repository":{"type":"git","url":"git://github.com/lemonde/utf8-binary-cutter.git"},"keywords":["utf-8","truncate"],"author":{"name":"Yvem","email":"jutard@lemonde.fr"},"license":"public domain","bugs":{"url":"https://github.com/lemonde/utf8-binary-cutter/issues"},"homepage":"https://github.com/lemonde/utf8-binary-cutter","dependencies":{"lodash":"^1.9.1"},"devDependencies":{"chai":"^1.9.1","lodash":"latest","mocha":"^1.20.1","sinon":"^1.10.2","sinon-chai":"^2.5.0"},"gitHead":"c6dd36a21f42a2d9e40005ce0370ea28a22bd4b0","_id":"utf8-binary-cutter@0.8.0","_shasum":"f971913a6a9f5f432e9a5fee703cbd6fdb22a2fd","_from":".","_npmVersion":"1.4.23","_npmUser":{"name":"offirmo","email":"offirmo.net@gmail.com"},"maintainers":[{"name":"offirmo","email":"offirmo.net@gmail.com"}],"dist":{"shasum":"f971913a6a9f5f432e9a5fee703cbd6fdb22a2fd","tarball":"http://registry-npm.ywwl.com/utf8-binary-cutter/download/utf8-binary-cutter-0.8.0.tgz","integrity":"sha512-XrGJUXY2Mwc7HQeTaK1Z1AVB4LatPleP54PZcnqmsxsiJhfuXvnCwrjurAWLcdVskE2mS5BcAnhJfgIeqr1iEA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIByRewWTN04u/zOMRBquuMxsAB95YWcpIfxZzh+TolUCAiEAmFvmSQ/Pe46awB2tXrA2dKhJslfYsMjZljGdECh69x4="}],"size":25187,"noattachment":false,"key":"/utf8-binary-cutter/-/utf8-binary-cutter-0.8.0.tgz"},"directories":{},"readme":"utf8-binary-cutter\n==================\n\n[![a cutter](https://raw.githubusercontent.com/lemonde/utf8-binary-cutter/master/doc/1390-Straight-cutter-18mm.jpg)](https://github.com/lemonde/utf8-binary-cutter)\n\nA small node.js lib to truncate UTF-8 strings to a given binary size. Useful when dealing with old systems handling UTF-8 as ascii/latin-1, for ex. MySQL or Oracle database.\n\nInteresting reads :\n* [About MySQL, UTF-8 and saving headaches](http://melp.nl/2011/01/about-mysql-utf-8-and-saving-headaches/)\n* [Getting out of MySQL Character Set Hell](https://www.bluebox.net/insight/blog-article/getting-out-of-mysql-character-set-hell)\n\nUsage\n=====\n* Works on UTF-8 strings (javascript strings are UTF-8 unless you're doing fancy things)\n\n```javascript\nvar Cutter = require('utf8-binary-cutter');\n```\n\n* `getBinarySize()` : returns the binary size of the given string\n\n```javascript\nvar utf8String = 'abc☃☃☃'; // abc then 3 times the UTF-8 « snowman » char which takes 3 bytes\n\nconsole.log( Cutter.getBinarySize( utf8String ) );  // 12 = 1 + 1 + 1 + 3 + 3 + 3\n```\n\n* `truncateToBinarySize()` truncate so that final binary size is lower or equal than the given limit :\n\n```javascript\nvar utf8String = 'abc☃☃☃'; // abc then 3 times the UTF-8 « snowman » char which takes 3 bytes\n\nconsole.log( Cutter.truncateToBinarySize( utf8String, 20 ) ); // 'abc☃☃☃'  -> no change\nconsole.log( Cutter.truncateToBinarySize( utf8String, 12 ) ); // 'abc☃☃☃'  -> no change\nconsole.log( Cutter.truncateToBinarySize( utf8String, 11 ) ); // 'abc☃...' -> to avoid cutting utf8 chars,\n  // the two last snowmen had to be removed. Final size = 9 bytes\nconsole.log( Cutter.truncateToBinarySize( utf8String, 10 ) ); // 'abc☃...' -> idem\nconsole.log( Cutter.truncateToBinarySize( utf8String,  9 ) ); // 'abc☃...' -> idem\nconsole.log( Cutter.truncateToBinarySize( utf8String,  8 ) ); // 'abc...'\n```\n\n* `truncateFieldsToBinarySize()` multiple truncations at the same time :\n  * NOTE : returns a new object.\n  * NOTE : iterates only on own properties\n  * NOTE : only truncated strings are copied, other members are shared with original object.\n\n```javascript\nvar maxBinarySizes = {\n  title: 40,\n  content: 200\n};\n\nconsole.log( Cutter.truncateFieldsToBinarySize({\n    title: '☃☃☃ A véry véry long title with UTF-8 ☃☃☃',\n    content: 'I ❤ utf8-binary-cutter !',\n    foo: 42\n  },\n  maxBinarySizes\n));\n\n--> {\n  title: '☃☃☃ A véry véry long title wi...',\n  content: 'I ❤ utf8-binary-cutter !',\n  foo: 42\n}\n```\n\n\n* `truncateToCharLength()` normal truncate is also provided for convenience : truncate so that final char length is lower or equal than the given limit :\n\n```javascript\nvar utf8String = 'abc☃☃☃'; // 6 chars\n\nconsole.log( Cutter.truncateToCharLength( utf8String, 10 ) ); // 'abc☃☃☃'  -> no change\nconsole.log( Cutter.truncateToBinarySize( utf8String,  6 ) ); // 'abc☃☃☃'  -> no change\nconsole.log( Cutter.truncateToBinarySize( utf8String,  5 ) ); // 'ab...'   -> 5 chars, ok\n```\n\n* optional callback when truncating (useful for logging) :\n\n```\ntruncateToBinarySize(foo, 42, function(maxBinarySize, originalString, truncatedString) {\n  logger.warn(...\n});\n\ntruncateToCharLength(foo, 42, function(maxCharLength, originalString, truncatedString) {\n  logger.warn(...\n});\n\nCutter.truncateFieldsToBinarySize({\n    title: '☃☃☃ A véry véry long title with UTF-8 ☃☃☃',\n    content: 'I ❤ utf8-binary-cutter !',\n    foo: 42\n  },\n  // maxBinarySizes\n  {\n    title: 40,\n    content: 200\n  },\n  // callback\n  // will be called for each member truncated.\n  // 4th param : the key of the member being truncated.\n  function(maxCharLength, originalString, truncatedString, key) {\n    logger.warn(...\n  }\n);\n\n```\n\nContributing\n============\n* clone repo\n* ensure your editor is decent and pick up the `.editorconfig` and `.jshintrc` files\n* `npm install`\n* `npm test`\n* add tests, add features, send a PR\n\nThanks !","publish_time":1418055513164,"_cnpm_publish_time":1418055513164,"dist-tags":{"latest":"0.9.2"}}