{"_id":"@alttiri/string-magic","_rev":"2-546e5a1363d41c055be98c3b9933e457","name":"@alttiri/string-magic","dist-tags":{"latest":"0.3.0-20240821"},"versions":{"0.1.0-20240707":{"name":"@alttiri/string-magic","version":"0.1.0-20240707","keywords":["string","util","url","title","hostname","cleaner"],"author":{"name":"gh.alttiri"},"license":"MIT","_id":"@alttiri/string-magic@0.1.0-20240707","maintainers":[{"name":"alttiri","email":"AlttiRi@pm.me"}],"homepage":"https://github.com/AlttiRi/string-magic","bugs":{"url":"https://github.com/alttiri/string-magic/issues"},"dist":{"shasum":"ce0c55753259e5f79f11346d19f34da650e65bdf","tarball":"https://registry.npmjs.org/@alttiri/string-magic/-/string-magic-0.1.0-20240707.tgz","fileCount":10,"integrity":"sha512-ByOtroGzRvS9vXx6BdzMh5j2NZM3zEXdBqE+wNkq/7wPdAo3VyS9nXQr1Sg4W624tp7/c8kU5qnnhRJgY4MnJQ==","signatures":[{"sig":"MEQCIBnhPTbr9IwMYLJ0F3pk70uUPVpx6AbRnO8TRlyx+lP6AiBoNvL+khOuTFArCOOJ06XR93Q2rpP3ZQcHJ1AyZ72a5A==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":24766},"main":"index.js","type":"module","gitHead":"aed37977ce45775b6154f6ddcae683c5ada00a30","scripts":{"#":"","##":"","###":"","clean-ts":"tsc --build --clean","login-ghp":"npm login --registry=https://npm.pkg.github.com","login-npm":"npm login --registry=https://registry.npmjs.org","compile-ts":"tsc --build","publish-ghp":"npm publish --registry=https://npm.pkg.github.com","publish-npm":"npm publish --registry=https://registry.npmjs.org","test-publish":"npm pack","unpublish-npm":"npm unpublish @alttiri/string-magic@0.1.0-20240707 --registry=https://registry.npmjs.org","compile-ts-watch":"tsc --build --watch"},"typings":"index.d.ts","_npmUser":{"name":"alttiri","email":"AlttiRi@pm.me"},"repository":{"url":"git+https://github.com/alttiri/string-magic.git","type":"git"},"_npmVersion":"10.5.2","description":"It's not a magic string, it's a string magic. Some util StringRule-driven functions do some string transformations.","directories":{},"_nodeVersion":"20.13.1","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"typescript":"5.5.3","@types/node":"20.12.12","@alttiri/util-js":"1.10.2-20240603","@alttiri/util-node-js":"2.1.0-20240624"},"_npmOperationalInternal":{"tmp":"tmp/string-magic_0.1.0-20240707_1720328052769_0.8124884697927313","host":"s3://npm-registry-packages"}},"0.2.0-20240725":{"name":"@alttiri/string-magic","version":"0.2.0-20240725","keywords":["string","util","url","title","hostname","cleaner"],"license":"MIT","_id":"@alttiri/string-magic@0.2.0-20240725","maintainers":[{"name":"alttiri","email":"AlttiRi@pm.me"}],"homepage":"https://github.com/AlttiRi/string-magic","bugs":{"url":"https://github.com/alttiri/string-magic/issues"},"dist":{"shasum":"5abcd1f192f1e80989f1d562875d8f5ed510730f","tarball":"https://registry.npmjs.org/@alttiri/string-magic/-/string-magic-0.2.0-20240725.tgz","fileCount":10,"integrity":"sha512-Wfl/t9s6p0PE25xHH8HUL6KIIrnEtNC4Jt0MikaE+3vqdL4alKAJMDm8tG38bNa15yh9o+GhgfPS/J+pQySPTg==","signatures":[{"sig":"MEUCIQD3/+mdTMIzLfBWiZ4Wp59TemgEYdTZd6z44JWTVJduOAIgLKZUVPAPdMC44gvHdd2TEvtMWbwye02FkV81VAnPMo4=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":24810},"main":"index.js","type":"module","gitHead":"9e0c2255999253f5c03e0aee4d06609aede73ecc","scripts":{"#":"","##":"","###":"","clean-ts":"tsc --build --clean","login-ghp":"npm login --registry=https://npm.pkg.github.com","login-npm":"npm login --registry=https://registry.npmjs.org","compile-ts":"tsc --build","publish-ghp":"tsc --build && npm publish --registry=https://npm.pkg.github.com","publish-npm":"tsc --build && npm publish --registry=https://registry.npmjs.org","test-publish":"tsc --build && npm pack","unpublish-npm":"npm unpublish @alttiri/string-magic@0.1.0-20240707 --registry=https://registry.npmjs.org","compile-ts-watch":"tsc --build --watch"},"typings":"index.d.ts","_npmUser":{"name":"alttiri","email":"AlttiRi@pm.me"},"repository":{"url":"git+https://github.com/alttiri/string-magic.git","type":"git"},"_npmVersion":"10.7.0","description":"It's not a magic string, it's a string magic. Some util StringRule-driven functions do some string transformations.","directories":{},"_nodeVersion":"20.15.1","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"typescript":"5.5.4","@types/node":"20.12.12","@alttiri/util-js":"1.13.1-20240725","@alttiri/util-node-js":"2.1.1-20240725"},"_npmOperationalInternal":{"tmp":"tmp/string-magic_0.2.0-20240725_1721901407815_0.7635621704576763","host":"s3://npm-registry-packages"}},"0.3.0-20240821":{"name":"@alttiri/string-magic","version":"0.3.0-20240821","description":"It's not a magic string, it's a string magic. Some util StringRule-driven functions do some string transformations.","homepage":"https://github.com/AlttiRi/string-magic","keywords":["string","util","url","title","hostname","cleaner"],"license":"MIT","type":"module","main":"index.js","typings":"index.d.ts","repository":{"type":"git","url":"git+https://github.com/alttiri/string-magic.git"},"publishConfig":{"access":"public"},"scripts":{"compile-ts":"tsc --build","compile-ts-watch":"tsc --build --watch","clean-ts":"tsc --build --clean","#":"","test-publish":"tsc --build && npm pack","publish-npm":"tsc --build && npm publish --registry=https://registry.npmjs.org","publish-ghp":"tsc --build && npm publish --registry=https://npm.pkg.github.com","##":"","unpublish-npm":"npm unpublish @alttiri/string-magic@0.1.0-20240707 --registry=https://registry.npmjs.org","###":"","login-npm":"npm login --registry=https://registry.npmjs.org","login-ghp":"npm login --registry=https://npm.pkg.github.com"},"devDependencies":{"@alttiri/util-js":"1.13.1-20240725","@alttiri/util-node-js":"2.1.1-20240725","@types/node":"20.12.12","typescript":"5.5.4"},"_id":"@alttiri/string-magic@0.3.0-20240821","gitHead":"f03b272d92c9fb657c5747957f29839329dfaf4e","bugs":{"url":"https://github.com/alttiri/string-magic/issues"},"_nodeVersion":"20.15.1","_npmVersion":"10.7.0","dist":{"integrity":"sha512-682EoufxJn0yT2uzgSmamOjYFi7UsQZnZlraAmp5YKakh4Afzi+bHgM6SYN4YrQf8Q/8BgQPaO90ZYj2YL0GqQ==","shasum":"f57ddd2352c217f2c68d35ea09d5a43cffe47c2d","tarball":"https://registry.npmjs.org/@alttiri/string-magic/-/string-magic-0.3.0-20240821.tgz","fileCount":10,"unpackedSize":29612,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEatH4CIgVJmnwB3NFvbVNc8fXyvJCMtMACX5YWmnSCcAiEAq1JptbnCokrQ4KSDhdvrl4B58kxCUP+pY5kGs6Xxs6g="}]},"_npmUser":{"name":"alttiri","email":"AlttiRi@pm.me"},"directories":{},"maintainers":[{"name":"alttiri","email":"AlttiRi@pm.me"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/string-magic_0.3.0-20240821_1724249443770_0.9639438624284578"},"_hasShrinkwrap":false}},"time":{"created":"2024-07-07T04:54:12.614Z","modified":"2024-08-21T14:10:44.094Z","0.1.0-20240707":"2024-07-07T04:54:12.946Z","0.2.0-20240725":"2024-07-25T09:56:47.951Z","0.3.0-20240821":"2024-08-21T14:10:43.929Z"},"bugs":{"url":"https://github.com/alttiri/string-magic/issues"},"license":"MIT","homepage":"https://github.com/AlttiRi/string-magic","keywords":["string","util","url","title","hostname","cleaner"],"repository":{"type":"git","url":"git+https://github.com/alttiri/string-magic.git"},"description":"It's not a magic string, it's a string magic. Some util StringRule-driven functions do some string transformations.","maintainers":[{"name":"alttiri","email":"AlttiRi@pm.me"}],"readme":"# string-magic\r\n\r\n~~ It's not a magic string, it's a string magic. ~~\r\n\r\nSome util \"StringRule\"-driven functions do some string transformations.\r\n\r\nI think, this lib will be mostly for my personal use, so the readme is short.\r\n\r\n---\r\n\r\nThere are two string cleaners — `TitleCleaner` and `UrlCleaner`.\r\n\r\nThe first one is to clean sites' titles from a boilerplate text like `\"on Twitter\"`,\r\nthe second one is to clean URLs (to remove search params and do some other things like removing site's redirects).\r\n\r\nYou describe rules as a string array, then apply them on a string is associated with some URL.\r\n\r\n---\r\n\r\n## TitleCleaner\r\n\r\n```ts\r\nconst tcRules: TCRuleStrings = [\r\n    \"site:artstation.com\",\r\n      \"trim-start:: ArtStation - \",\r\n    \"site:deviantart.com\",\r\n      \"trim-end:: on DeviantArt\",\r\n];\r\nconst titleCleaner = TitleCleaner.fromRuleStrings(tcRules);\r\n\r\ntitleCleaner.clean(\r\n    \"https://www.artstation.com/artwork/o0Yxm\",\r\n    \"ArtStation - Overwatch Preorder Widowmaker Noire\"\r\n);\r\n//  \"Overwatch Preorder Widowmaker Noire\"\r\n```\r\n\r\n## UrlCleaner\r\n```ts\r\nconst ucRules: UCRuleStrings = [\r\n    \"site:youtube.com\",\r\n      \"trim-search-params:feature t si list\",\r\n    \"site:t.umblr.com\",\r\n      \"filter-start:https://t.umblr.com/redirect\",\r\n      \"search-param:z\",\r\n      \"recursive\",\r\n    \"site:deviantart.com\",\r\n      \"trim-start:https://www.deviantart.com/users/outgoing?\",\r\n      \"decode-url\",\r\n      \"recursive\",\r\n];\r\nconst urlCleaner = UrlCleaner.fromRuleStrings(ucRules);\r\n\r\nurlCleaner.clean(\"https://t.umblr.com/redirect?z=http%3A%2F%2Fgfycat.com%2FRedFatCat&m=1\");\r\n// \"https://gfycat.com/RedFatCat\"\r\n\r\nurlCleaner.clean(\"http://www.youtube.com/watch?feature=player_embedded&v=z_HWtzUHm6s\");\r\n// \"http://www.youtube.com/watch?v=z_HWtzUHm6s\"\r\n\r\nurlCleaner.clean(\"https://www.deviantart.com/users/outgoing?https://t.umblr.com/redirect?z=https%3A%2F%2Ftwitter.com%2FSpaceX%2Fstatus%2F1798792222743122164\");\r\n// \"https://twitter.com/SpaceX/status/1798792222743122164\"\r\n\r\n```\r\n\r\n---\r\n\r\nSee tests for more examples.\r\n\r\n- [TitleCleaner](https://github.com/AlttiRi/string-magic/tree/master/test/title-cleaner)\r\n- [UrlCleaner](https://github.com/AlttiRi/string-magic/tree/master/test/url-cleaner)\r\n\r\n---\r\n\r\n## *.d.ts\r\n\r\n```ts\r\ndeclare const TypeArray_TCCommands: readonly [\"trim-start\", \"trim-end\", \"trim-start-end\"];\r\ndeclare const TypeArray_TCCommands_SD: readonly [\"trim-regex\"];\r\nexport type TCCommandString = typeof TypeArray_TCCommands[number];\r\nexport type TCCommandString_SD = typeof TypeArray_TCCommands_SD[number];\r\nexport type TCRuleString = `${TCCommandString | TCCommandString_SD | \"sites\" | \"site\"}:${string}`;\r\nexport type TCRuleStrings = TCRuleString[];\r\nexport type TCRule = {\r\n    command: TCCommandString;\r\n    data: string[];\r\n} | {\r\n    command: TCCommandString_SD;\r\n    data: string;\r\n};\r\nexport type TCRuleRecords = Record<string, Array<TCRule>>;\r\nexport type TCCompiledRules = {\r\n    ruleRecords: TCRuleRecords;\r\n    ruleRecordsWC: TCRuleRecords | null;\r\n};\r\nexport declare function isTCRuleStringArray(array: string[]): array is TCRuleStrings;\r\nexport declare const knownCommands: Set<string>;\r\nexport declare const knownCommands_SD: Set<string>;\r\nexport declare class TitleCleaner {\r\n    private readonly ruleRecords;\r\n    private readonly ruleRecordsWC;\r\n    private constructor();\r\n    static fromRuleStrings(rule_strings: TCRuleStrings): TitleCleaner;\r\n    static fromRuleRecords(rules: TCCompiledRules): TitleCleaner;\r\n    static compileRuleStrings(rule_strings: TCRuleStrings): TCCompiledRules;\r\n    private getRules;\r\n    clean(url: string, title: string): string;\r\n    private applyRule;\r\n    private static parseRuleString;\r\n}\r\n\r\ndeclare const TypeArray_UCRuleCommands: readonly [\"https\", \"decode-url\", \"recursive\", \"atob\"];\r\ntype UCCommandString = typeof TypeArray_UCRuleCommands[number];\r\ndeclare const TypeArray_UCRuleDataCommands: readonly [\"filter-start\", \"trim-start\", \"prepend\", \"trim-regex\", \"trim-search-param\", \"search-param\"];\r\ntype UCDataCommandString = typeof TypeArray_UCRuleDataCommands[number];\r\ndeclare const TypeArray_UCRuleMDataCommands: readonly [\"trim-search-params\"];\r\ntype UCMDataCommandString = typeof TypeArray_UCRuleMDataCommands[number];\r\ntype UCAnyDataCommandString = UCDataCommandString | UCMDataCommandString;\r\ntype UCRuleCommandString = UCCommandString;\r\ntype UCRuleDataCommandString = `site:${string}` | `sites:${string}` | `${UCAnyDataCommandString}:${string}`;\r\nexport type UCRuleString = UCRuleCommandString | UCRuleDataCommandString;\r\nexport type UCRuleStrings = UCRuleString[];\r\ntype UCRule = {\r\n    command: UCCommandString;\r\n};\r\ntype UCDataRule = {\r\n    command: UCDataCommandString;\r\n    data: string;\r\n};\r\ntype UCMDataRule = {\r\n    command: UCMDataCommandString;\r\n    data: string | string[];\r\n};\r\ntype UCAnyDataRule = UCDataRule | UCMDataRule;\r\ntype UCAnyRule = UCRule | UCAnyDataRule;\r\nexport type UCRuleRecords = Record<string, Array<UCAnyRule>>;\r\nexport type UCCompiledRules = {\r\n    ruleRecords: UCRuleRecords;\r\n    ruleRecordsWC: UCRuleRecords | null;\r\n};\r\nexport declare function isUCRuleStringArray(array: string[]): array is UCRuleStrings;\r\nexport declare class UrlCleaner {\r\n    private readonly ruleRecords;\r\n    private readonly ruleRecordsWC;\r\n    private constructor();\r\n    static fromRuleStrings(rule_strings: UCRuleStrings): UrlCleaner;\r\n    static fromRuleRecords(rules: UCCompiledRules): UrlCleaner;\r\n    static compileRuleStrings(rule_strings: UCRuleStrings): UCCompiledRules;\r\n    private getRules;\r\n    clean(url: string): string;\r\n    private static parseRuleString;\r\n}\r\n\r\nexport declare function isPlainObjectEmpty(obj: object): boolean;\r\nexport declare function noWWW(hostname: string): string;\r\nexport declare function getHostname(url: string): string;\r\nexport declare function getHostnameWithURL(url: string): string;\r\n/** Find dot positions in a string. */\r\nexport declare function findDots(str: string): number[];\r\n/**\r\n * @example\r\n * getParentSubHosts(\"localhost\")    -> []\r\n * getParentSubHosts(\"example.com\")  -> []\r\n * getParentSubHosts(\"qwerty.example.com\")      -> [ \"example.com\" ]\r\n * getParentSubHosts(\"test.qwerty.example.com\") -> [ \"example.com\", \"qwerty.example.com\" ]\r\n */\r\nexport declare function getParentSubHosts(hostname: string): string[];\r\n/**\r\n * # Hostname rule matching\r\n *\r\n * Simple and performance oriented implementation with simplified WildCards support.\r\n * Only one (the first) hostname match's rules are applied.\r\n *\r\n * - \"example.com\"\r\n *  1. look for \"example.com\" rules, if none then do nothing (since, there are only 2 hostname parts).\r\n *\r\n *  - \"www.example.com\"\r\n *  1. look for \"example.com\" rules, if none then\r\n *  2. look for \"*.example.com\" rules.\r\n *\r\n *  - \"qwerty.example.com\"\r\n *  1. look for \"qwerty.example.com\", if none then\r\n *  2. look for \"*.example.com\".\r\n *\r\n *  - \"more.qwerty.example.com\"\r\n *  1. look for \"more.qwerty.example.com\", if none then\r\n *  2. look for \"*.example.com\", if none then\r\n *  3. look for \"*.qwerty.example.com\".\r\n */\r\n```\r\n","readmeFilename":"README.md"}