{"id":20473537,"url":"https://github.com/xpl/printable-characters","last_synced_at":"2025-04-13T11:30:20.620Z","repository":{"id":57330537,"uuid":"95902441","full_name":"xpl/printable-characters","owner":"xpl","description":"A little helper for handling strings containing zero width characters, ANSI styling, whitespaces, newlines, 💩, etc.","archived":false,"fork":false,"pushed_at":"2018-07-16T22:07:06.000Z","size":96,"stargazers_count":20,"open_issues_count":1,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T10:11:00.154Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/printable-characters","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xpl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-30T15:42:04.000Z","updated_at":"2024-10-26T17:41:56.000Z","dependencies_parsed_at":"2022-09-09T08:12:49.161Z","dependency_job_id":null,"html_url":"https://github.com/xpl/printable-characters","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fprintable-characters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fprintable-characters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fprintable-characters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fprintable-characters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xpl","download_url":"https://codeload.github.com/xpl/printable-characters/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248705468,"owners_count":21148544,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T14:25:47.658Z","updated_at":"2025-04-13T11:30:20.590Z","avatar_url":"https://github.com/xpl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# printable-characters\n\n[![Build Status](https://travis-ci.org/xpl/printable-characters.svg?branch=master)](https://travis-ci.org/xpl/printable-characters) [![Coverage Status](https://coveralls.io/repos/github/xpl/printable-characters/badge.svg)](https://coveralls.io/github/xpl/printable-characters) [![npm](https://img.shields.io/npm/v/printable-characters.svg)](https://npmjs.com/package/printable-characters) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/xpl/printable-characters.svg)](https://scrutinizer-ci.com/g/xpl/printable-characters/?branch=master) [![dependencies Status](https://david-dm.org/xpl/printable-characters/status.svg)](https://david-dm.org/xpl/printable-characters)\n\nA little helper for handling strings containing zero width characters, ANSI styling, whitespaces, newlines, [weird Unicode 💩 symbols](http://blog.jonnew.com/posts/poo-dot-length-equals-two), etc.\n\n## Determining the real (visible) length of a string\n\n```javascript\nconst { strlen } = require ('printable-characters')\n\nstrlen ('foo bar') // === 7\nstrlen ('\\u001b[106mfoo bar\\u001b[49m') // === 7\n```\n\n## Detecting blank text\n\n```javascript\nconst { isBlank } = require ('printable-characters')\n\nisBlank ('foobar') // === false\nisBlank ('\\u001b[106m  \\t  \\t   \\n     \\u001b[49m') // === true\n```\n\n## Obtaining a blank string of the same width\n\n```javascript\nconst { blank } = require ('printable-characters')\n\nblank ('💩')          // === ' '\nblank ('foo')         // === '   '\nblank ('\\tfoo \\nfoo') // === '\\t    \\n   '\nblank ('\\u001b[22m\\u001b[1mfoo \\t\\u001b[39m\\u001b[22m')) // === '    \\t'\n```\n\n## Matching invisible characters\n\n```javascript\nconst { ansiEscapeCodes, zeroWidthCharacters } = require ('printable-characters')\n\nconst s = '\\u001b[106m' + 'foo' + '\\n' + 'bar' + '\\u001b[49m'\n\ns.replace (ansiEscapeCodes, '')     // === 'foo\\nbar'\n .replace (zeroWidthCharacters, '') // === 'foobar'\n```\n\n## Getting the first N visible symbols, preserving the invisible parts\n\nUse for safely truncating strings to maximum width without breaking ANSI codes:\n\n```javascript\nconst { first } = require ('printable-characters')\n\nconst s = '\\u001b[22mfoobar\\u001b[22m'\n\nfirst (s, 0) // === '\\u001b[22m\\u001b[22m'\nfirst (s, 1) // === '\\u001b[22mf\\u001b[22m'\nfirst (s, 3) // === '\\u001b[22mfoo\\u001b[22m'\nfirst (s, 6) // === '\\u001b[22mfoobar\\u001b[22m'\n```\n\n## Extracting the invisible parts followed by the visible ones (parsing)\n\n```javascript\nconst { partition } = require ('printable-characters')\n\npartition ('')                        // [                                                     ])\npartition ('foo')                     // [['',          'foo']                                 ])\npartition ('\\u001b[1mfoo')            // [['\\u001b[1m', 'foo']                                 ])\npartition ('\\u001b[1mfoo\\u0000bar')   // [['\\u001b[1m', 'foo'],   ['\\u0000', 'bar']            ])\npartition ('\\u001b[1mfoo\\u0000bar\\n') // [['\\u001b[1m', 'foo'],   ['\\u0000', 'bar'], ['\\n', '']])\n```\n\n## Applications\n\n- [as-table](https://github.com/xpl/as-table) — a simple function that prints objects as ASCII tables\n- [string.bullet](https://github.com/xpl/string.bullet) — ASCII-mode bulleting for the list-style data\n- [string.ify](https://github.com/xpl/string.ify) — a fancy pretty printer for the JavaScript entities\n- [Ololog!](https://github.com/xpl/ololog) — a better `console.log` for the log-driven debugging junkies!\n\n## TODO\n\nHandle multi-component emojis, as in [this article](http://blog.jonnew.com/posts/poo-dot-length-equals-two):\n\n```javascript\nassert.equal (strlen ('👩‍❤️‍💋‍👩'), 1)  // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution\nassert.equal (blank ('👩‍❤️‍💋‍👩'), ' ') // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpl%2Fprintable-characters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxpl%2Fprintable-characters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpl%2Fprintable-characters/lists"}