{"id":16547539,"url":"https://github.com/waiting-for-dev/string-direction","last_synced_at":"2025-09-01T03:09:11.425Z","repository":{"id":56896779,"uuid":"10935243","full_name":"waiting-for-dev/string-direction","owner":"waiting-for-dev","description":"Automatic detection of text direction (ltr, rtl or bidi) for strings in Ruby","archived":false,"fork":false,"pushed_at":"2022-08-20T06:01:54.000Z","size":112,"stargazers_count":15,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-08-04T18:51:39.181Z","etag":null,"topics":["bidi","ruby-gem","scripts"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/waiting-for-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"COPYING.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"waiting-for-dev"}},"created_at":"2013-06-25T10:18:32.000Z","updated_at":"2022-10-12T07:33:26.000Z","dependencies_parsed_at":"2022-08-20T17:10:30.375Z","dependency_job_id":null,"html_url":"https://github.com/waiting-for-dev/string-direction","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/waiting-for-dev/string-direction","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fstring-direction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fstring-direction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fstring-direction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fstring-direction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/waiting-for-dev","download_url":"https://codeload.github.com/waiting-for-dev/string-direction/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fstring-direction/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273068857,"owners_count":25039911,"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","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["bidi","ruby-gem","scripts"],"created_at":"2024-10-11T19:14:39.308Z","updated_at":"2025-09-01T03:09:11.408Z","avatar_url":"https://github.com/waiting-for-dev.png","language":"Ruby","funding_links":["https://github.com/sponsors/waiting-for-dev"],"categories":[],"sub_categories":[],"readme":"# string-direction\n\n`string-direction` is a ruby library for automatic detection of the direction (left-to-right, right-to-left or bi-directional) in which a text should be displayed.\n\n## Overview\n\n```ruby\nrequire 'string-direction'\n\ndetector = StringDirection::Detector.new\n\ndetector.direction('english') #=\u003e 'ltr'\ndetector.direction('العربية') #=\u003e 'rtl'\ndetector.direction(\"english العربية\") #=\u003e 'bidi'\n\ndetector.ltr?('english') #=\u003e true\ndetector.rtl?('العربية') #=\u003e true\ndetector.bidi?('english') #=\u003e false\n```\n\nBut, if you prefer, you can monkey patch `String`:\n\n```ruby\nString.send(:include, StringDirection::StringMethods)\n\n'english'.direction #=\u003e 'ltr'\n'العربية'.rtl? #=\u003e true\n```\n\n## Strategies\n\n`string-direction` uses different strategies in order to try to detect the direction of a string. The detector uses them once at a time and returns the result once one of them succeeds, aborting any further analysis.\n\nStrategies are passed to the detector during its initialization:\n\n```ruby\ndetector = StringDirection::Detector.new(:foo, :bar)\n```\n\nIn the above example, classes `StringDirection::FooStrategy` and `StringDirection::BarStrategy` have to be in the load path.\n\nThree strategies are natively integrated: `marks`, `characters` \u0026 `dominant`. `marks` \u0026\u0026 `characters` are used as default strategies if no arguments are given to the detector.\n\n### marks\n\nLooks for the presence of Unicode direction marks: [left-to-right](http://en.wikipedia.org/wiki/Left-to-right_mark) (\\u200e) or [right-to-left](http://en.wikipedia.org/wiki/Right-to-left_mark) (\\u200f).\n\n```ruby\ndetector = StringDirection::Detector.new(:marks)\n\ndetector.direction(\"\\u200eالعربية\") #=\u003e \"ltr\"\ndetector.direction(\"\\u200fEnglish\") #=\u003e \"rtl\"\n```\n\n`marks` strategy can not only analyze a string but everything responding to `to_s`.\n\n### characters\n\nLooks for the presence of right-to-left characters in the scripts used in the string.\n\nBy default, `string-direction` consider following scripts to have a right-to-left writing:\n\n* Arabic\n* Hebrew\n* Nko\n* Kharoshthi\n* Phoenician\n* Syriac\n* Thaana\n* Tifinagh\n\n```ruby\ndetector = StringDirection::Detector.new(:characters)\n\ndetector.direction('english') #=\u003e 'ltr'\ndetector.direction('العربية') #=\u003e 'rtl'\n```\n\nYou can change these defaults:\n\n```ruby\ndetector.direction('ᚪᚫᚬᚭᚮᚯ') #=\u003e 'ltr'\n\nStringDirection.configure do |config|\n  config.rtl_scripts \u003c\u003c 'Runic'\nend\n\ndetector.direction('ᚪᚫᚬᚭᚮᚯ') #=\u003e 'rtl'\n```\n\nThis can be useful, mainly, for scripts that have both left-to-right and right-to-left representations:\n\n* Bopomofo\n* Carian\n* Cypriot\n* Lydian\n* Old_Italic\n* Runic\n* Ugaritic\n\nKeep in mind than only [scripts recognized by Ruby regular expressions](http://www.ruby-doc.org/core-1.9.3/Regexp.html#label-Character+Properties) are allowed.\n\n`characters` strategy can not only analyze a string but everything responding to `to_s`.\n\n### dominant\n\nWith `dominant` strategy, a string can be left-to-right or right-to-left, but never bidi. It returns one or the other in function of which one has more characters.\n\n```ruby\ndetector = StringDirection::Detector.new(:dominant)\n\ndetector.direction('e العربية') #=\u003e 'rtl'\ndetector.direction('english ة') #=\u003e 'ltr'\n```\n\nAs with `characters` strategy, you can change which scripts are considered right-to-left.\n\n`dominant` strategy can not only analyze a string but everything responding to `to_s`.\n\n### Custom Strategies\n\nYou can define your custom strategies. To do so, you just have to define a class inside `StringDirection` module with a name ending with `Strategy`. This class has to respond to an instance method `run` which takes the string as argument. You can inherit from `StringDirection::Strategy` to have convenient methods `ltr`, `rtl` and `bidi` which return expected result. If the strategy doesn't know the direction, it must return `nil`.\n\n```ruby\nclass StringDirection::AlwaysLtrStrategy \u003c StringDirection::Strategy\n  def run(string)\n    ltr\n  end\nend\n\ndetector = StringDirection::Detector.new(:always_ltr)\ndetector.direction('العربية') #=\u003e 'ltr'\n```\n\n### Changing default strategies\n\n`marks` and `characters` are default strategies, but you can change them:\n\n```ruby\nStringDirection.configure do |config|\n  config.default_strategies = [:custom, :marks, :always_ltr]\nend\n```\n\n## Monkey patching String\n\nIf you desire, you can monkey patch `String`:\n\n```ruby\nString.send(:include, StringDirection::StringMethods)\n\n'english'.direction #=\u003e 'ltr'\n```\n\nIn that case, strategies configured in `string_method_strategies` are used:\n\n```ruby\nStringDirection.configure do |config|\n  config.string_methods_strategies = [:marks, :characters]\nend\n```\n\n## Release Policy\n\n`string-direction` follows the principles of [semantic versioning](http://semver.org/).\n\n## Disclaimer\n\nI'm not an expert neither in World scripts nor in Unicode. So, please, if you know some case where this library is not working as it should be, [open an issue](https://github.com/laMarciana/string-direction/issues) and help improve it.\n\n## Thanks\n\n[Omniglot.com](http://www.omniglot.com/), where I learnt which Ruby recognized scripts have a right-to-left writing system.\n\n## LICENSE\n\nCopyright 2013 Marc Busqué - \u003cmarc@lamarciana.com\u003e\n\nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program.  If not, see \u003chttp://www.gnu.org/licenses/\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaiting-for-dev%2Fstring-direction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwaiting-for-dev%2Fstring-direction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaiting-for-dev%2Fstring-direction/lists"}