{"name":"utf8-binary-cutter","version":"0.9.2","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":"^4.17.10"},"devDependencies":{"chai":"~1.10.0","mocha":"~2.0.1","sinon":"~1.12.1","sinon-chai":"~2.6.0"},"gitHead":"9f8b2955bd888c73b7cdb3f1fc5de910a150ff25","_id":"utf8-binary-cutter@0.9.2","_npmVersion":"5.6.0","_nodeVersion":"8.9.1","_npmUser":{"name":"lemonde","email":"npm@lemonde.fr"},"dist":{"integrity":"sha512-lS/2TaA9idsyafus4+WaB+C/AfL3JD85C/sgMJBpplZay1G5SwTQcxmd4jiJLI1VxSJr6a3yuNicBxD+iU2MKQ==","shasum":"c2d28c7f3fd0387e5e162862d32e34090e2b96d9","tarball":"http://registry-npm.ywwl.com/utf8-binary-cutter/download/utf8-binary-cutter-0.9.2.tgz","fileCount":11,"unpackedSize":39871,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbI9n5CRA9TVsSAnZWagAAHTgP/2EFnSS5ueHV+uBcazGa\nTXWgLKm/botY1sjiX9DWDtZtG22Z/1qRtEhmmBqtTMAWxTA5Wu/i6uyr9wXP\n2Dxyqhg47mH398V3NErVUOFkH/RccWmNMVAXSCDwbDpYw8fUtxOlXRbc5g57\nEExnGvdCtp4TAFPLW41BG16k8uLQORam1Xn4R0f7O9Lor0760wIIDw1jccmU\nwmlUjIkE8yhNeJksk/QgaIt2LPPHp44Paq/Yg3Ll738EzTpQIu3U74PY3ICF\nCVMgWmNb3CxAcElhfBBDhDWhAXjpcqE6jIiUcn1iiYTSjAYMadJqRcD1F8xC\nPTaIab0KHZF/QIxrhafgg7liW6G2FtkkK/S+Pb9g23UdFiCf6n7gfdiGfIJJ\njgk56sXS0A+5oXxcZs+aZxl3dxn48XnO5m1F+MsMrkT6jN7pJbuMP9tNjuXa\nja59KAxNyEmhhL2fM2u6YE2b8cR2ce0y9AIY1SLMLYND2bN8zNcV5hK7l25y\ntF8oYHI0kAJokXNwzYq0i1SX4JxcsDGPDOlvY4j+NFKl6I1LctTi3lC+z+nB\nuoBSlUtK3zbAYHp3/FrrZT2SIvw7S3dxrB81MeNXwQWz3QmD+4wsmZZWzt4I\nttLdyaEOCXG8SRQuxLB06kT5gTTQoezQlKO+uFvBrvDirsj1BDqrxJThtdp4\nEt+F\r\n=Aefm\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIB+J5W326MkllIO7XpVy3A2kUjM6ZpssNs1xkwG/gLPXAiEAzrh4GXr2nQ8plP+flGmt711x9xag7ilAM8sBQ8AonzE="}],"size":25260,"noattachment":false,"key":"/utf8-binary-cutter/-/utf8-binary-cutter-0.9.2.tgz"},"maintainers":[{"name":"jbustin","email":"justin.bourrousse@ymail.com"},{"name":"lemonde","email":"npm@lemonde.fr"},{"name":"offirmo","email":"offirmo.net@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/utf8-binary-cutter_0.9.2_1529076215850_0.7173056010479559"},"_hasShrinkwrap":false,"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":1529076215936,"_cnpm_publish_time":1529076215936,"dist-tags":{"latest":"0.9.2"}}