From 335d833f35b18aefc78facaf24116f990f56126c Mon Sep 17 00:00:00 2001 From: Harry Brundage Date: Sun, 24 Nov 2024 17:50:22 -0500 Subject: [PATCH] Rename package in readme --- Makefile | 30 ------- README.md | 260 +++++++++++++++++++++++------------------------------- 2 files changed, 110 insertions(+), 180 deletions(-) delete mode 100644 Makefile diff --git a/Makefile b/Makefile deleted file mode 100644 index 4ba9ad7..0000000 --- a/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -BIN = ./node_modules/.bin - -install:: - @yarn install - -test:: - @yarn test - -build:: - @yarn build - -clean:: - @rm -rf dist - -release-patch:: clean test build - @$(call release,patch) - -release-minor:: clean test build - @$(call release,minor) - -release-major:: clean test build - @$(call release,major) - -publish:: - git push --tags origin HEAD:master - npm publish - -define release - npm version $(1) -m 'Release v%s' -endef diff --git a/README.md b/README.md index d4f6592..421460f 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,37 @@ -# inflected - -[![Travis build status](https://img.shields.io/travis/martinandert/inflected/master.svg)](https://travis-ci.org/martinandert/inflected) -[![npm downloads](https://img.shields.io/npm/dm/inflected.svg)](https://npmjs.com/package/inflected) -[![no dependencies](https://img.shields.io/badge/dependencies-none-brightgreen.svg)](https://npmjs.com/package/inflected) -[![license](https://img.shields.io/github/license/martinandert/inflected.svg)](https://github.com/martinandert/inflected/blob/master/LICENSE.txt) - -A port of ActiveSupport's inflector to Node.js. Also usable in the browser. +# superflected +A port of ActiveSupport's inflector to Node.js. Also usable in the browser. Forked from [inflected](https://github.com/martinandert/inflected) and modified to use TypeScript and improve performance. ## Installation Install via npm: ```bash -% npm install inflected +% npm install superflected ``` -Or via yarn: +Or via pnpm: ```bash -% yarn add inflected -``` - -The UMD build is also available on [unpkg](https://unpkg.com/), adding a `Inflector` object to the global scope. - -```html - +% pnpm add superflected ``` - ## Usage The module exports an object with several utility functions. ```js -var Inflector = require('inflected'); +import { pluralize } from "superflected"; -Inflector.pluralize('Category') // => 'Categories' -``` - -If using ES modules, you can cherry-pick only the functions you're interested in: - -```js -import { pluralize } from 'inflected'; - -pluralize('Category') // => 'Categories' +pluralize("Category"); // => 'Categories' ``` Here is the complete API reference: - -### Inflector.pluralize +### pluralize ```js -string pluralize(string word[, string locale]) +pluralize(word: string, locale?: string): string ``` Returns the plural form of the word in the string. @@ -61,19 +39,18 @@ Returns the plural form of the word in the string. If passed an optional `locale` parameter, the word will be pluralized using rules defined for that language. By default, this parameter is set to "en". ```js -Inflector.pluralize('post') // => 'posts' -Inflector.pluralize('octopus') // => 'octopi' -Inflector.pluralize('sheep') // => 'sheep' -Inflector.pluralize('words') // => 'words' -Inflector.pluralize('CamelOctopus') // => 'CamelOctopi' -Inflector.pluralize('ley', 'es') // => 'leyes' +pluralize("post"); // => 'posts' +pluralize("octopus"); // => 'octopi' +pluralize("sheep"); // => 'sheep' +pluralize("words"); // => 'words' +pluralize("CamelOctopus"); // => 'CamelOctopi' +pluralize("ley", "es"); // => 'leyes' ``` - -### Inflector.singularize +### singularize ```js -string singularize(string word[, string locale]) +singularize(word: string, locale?: string): string ``` The reverse of `pluralize`, returns the singular form of a word in a string. @@ -81,219 +58,207 @@ The reverse of `pluralize`, returns the singular form of a word in a string. If passed an optional `locale` parameter, the word will be singularized using rules defined for that language. By default, this parameter is set to "en". ```js -Inflector.singularize('posts') // => 'post' -Inflector.singularize('octopi') // => 'octopus' -Inflector.singularize('sheep') // => 'sheep' -Inflector.singularize('word') // => 'word' -Inflector.singularize('CamelOctopi') // => 'CamelOctopus' -Inflector.singularize('leyes', 'es') // => 'ley' +singularize("posts"); // => 'post' +singularize("octopi"); // => 'octopus' +singularize("sheep"); // => 'sheep' +singularize("word"); // => 'word' +singularize("CamelOctopi"); // => 'CamelOctopus' +singularize("leyes", "es"); // => 'ley' ``` - -### Inflector.camelize +### camelize ```js -string camelize(string term[, boolean uppercaseFirstLetter]) +camelize(term: string, uppercaseFirstLetter?: boolean): string ``` By default, `camelize` converts strings to UpperCamelCase. If the second argument is set to `false` then `camelize` produces lowerCamelCase. ```js -Inflector.camelize('foo_bar') // => 'FooBar' -Inflector.camelize('foo_bar', false) // => 'fooBar' +camelize("foo_bar"); // => 'FooBar' +camelize("foo_bar", false); // => 'fooBar' ``` As a rule of thumb you can think of `camelize` as the inverse of `underscore`, though there are cases where that does not hold: ```js -Inflector.camelize(Inflector.underscore('SSLError')) // => 'SslError' +camelize(underscore("SSLError")); // => 'SslError' ``` -### Inflector.underscore +### underscore ```js -string underscore(string camelCasedWord) +underscore(camelCasedWord: string): string ``` Makes an underscored, lowercase form from the expression in the string. ```js -Inflector.underscore('FooBar') // => 'foo_bar' +underscore("FooBar"); // => 'foo_bar' ``` As a rule of thumb you can think of `underscore` as the inverse of `camelize`, though there are cases where that does not hold: ```js -Inflector.camelize(Inflector.underscore('SSLError')) // => 'SslError' +camelize(underscore("SSLError")); // => 'SslError' ``` - -### Inflector.humanize +### humanize ```js -string humanize(string lowerCaseAndUnderscoredWord[, object options]) +humanize(lowerCaseAndUnderscoredWord: string, options?: object): string ``` -Capitalizes the first word, turns underscores into spaces, and strips a trailing "_id" if present. +Capitalizes the first word, turns underscores into spaces, and strips a trailing "\_id" if present. Like `titleize`, this is meant for creating pretty output. The capitalization of the first word can be turned off by setting the `capitalize` option key to `false`. By default, this option is `true`. ```js -Inflector.humanize('employee_salary') // => 'Employee salary' -Inflector.humanize('author_id') // => 'Author' -Inflector.humanize('author_id', { capitalize: false }) // => 'author' +humanize("employee_salary"); // => 'Employee salary' +humanize("author_id"); // => 'Author' +humanize("author_id", { capitalize: false }); // => 'author' ``` - -### Inflector.titleize +### titleize ```js -string titleize(string sentence) +titleize(sentence: string): string ``` Capitalizes all the words and replaces some characters in the string to create a nicer looking title. `titleize` is meant for creating pretty output. ```js -Inflector.titleize('man from the boondocks') // => 'Man From The Boondocks' -Inflector.titleize('x-men: the last stand') // => 'X Men: The Last Stand' -Inflector.titleize('TheManWithoutAPast') // => 'The Man Without A Past' -Inflector.titleize('raiders_of_the_lost_ark') // => 'Raiders Of The Lost Ark' +titleize("man from the boondocks"); // => 'Man From The Boondocks' +titleize("x-men: the last stand"); // => 'X Men: The Last Stand' +titleize("TheManWithoutAPast"); // => 'The Man Without A Past' +titleize("raiders_of_the_lost_ark"); // => 'Raiders Of The Lost Ark' ``` - -### Inflector.tableize +### tableize ```js -string tableize(string className) +tableize(className: string): string ``` Create the name of a table like Rails does for models to table names. This method uses the `pluralize` method on the last word in the string. ```js -Inflector.tableize('RawScaledScorer') // => 'raw_scaled_scorers' -Inflector.tableize('egg_and_ham') // => 'egg_and_hams' -Inflector.tableize('fancyCategory') // => 'fancy_categories' +tableize("RawScaledScorer"); // => 'raw_scaled_scorers' +tableize("egg_and_ham"); // => 'egg_and_hams' +tableize("fancyCategory"); // => 'fancy_categories' ``` - -### Inflector.classify +### classify ```js -string classify(string tableName) +classify(tableName: string): string ``` Create a class name from a plural table name like Rails does for table names to models. ```js -Inflector.classify('egg_and_hams') // => 'EggAndHam' -Inflector.classify('posts') // => 'Post' +classify("egg_and_hams"); // => 'EggAndHam' +classify("posts"); // => 'Post' ``` Singular names are not handled correctly: ```js -Inflector.classify('business') // => 'Busines' +classify("business"); // => 'Busines' ``` - -### Inflector.dasherize +### dasherize ```js -string dasherize(string underscoredWord) +dasherize(underscoredWord: string): string ``` Replaces underscores with dashes in the string. ```js -Inflector.dasherize('puni_puni') // => 'puni-puni' +dasherize("puni_puni"); // => 'puni-puni' ``` - -### Inflector.foreignKey +### foreignKey ```js -string foreignKey(string className[, boolean separateClassNameAndIdWithUnderscore]) +foreignKey(className: string, separateClassNameAndIdWithUnderscore?: boolean): string ``` -Creates a foreign key name from a class name. `separateClassNameAndIdWithUnderscore` sets whether the method should put "_" between the name and "id" (default: `true`). +Creates a foreign key name from a class name. `separateClassNameAndIdWithUnderscore` sets whether the method should put "\_" between the name and "id" (default: `true`). ```js -Inflector.foreignKey('Message') // => 'message_id' -Inflector.foreignKey('Message', false) // => 'messageid' +foreignKey("Message"); // => 'message_id' +foreignKey("Message", false); // => 'messageid' ``` - -### Inflector.ordinal +### ordinal ```js -string ordinal(object number) +ordinal(number: number): string ``` Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. ```js -Inflector.ordinal(1) // => 'st' -Inflector.ordinal(2) // => 'nd' -Inflector.ordinal(1002) // => 'nd' -Inflector.ordinal(1003) // => 'rd' -Inflector.ordinal(-11) // => 'th' -Inflector.ordinal(-1021) // => 'st' +ordinal(1); // => 'st' +ordinal(2); // => 'nd' +ordinal(1002); // => 'nd' +ordinal(1003); // => 'rd' +ordinal(-11); // => 'th' +ordinal(-1021); // => 'st' ``` - -### Inflector.ordinalize +### ordinalize ```js -string ordinalize(object number) +ordinalize(number: number): string ``` Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. ```js -Inflector.ordinalize(1) // => '1st' -Inflector.ordinalize(2) // => '2nd' -Inflector.ordinalize(1002) // => '1002nd' -Inflector.ordinalize(1003) // => '1003rd' -Inflector.ordinalize(-11) // => '-11th' -Inflector.ordinalize(-1021) // => '-1021st' +ordinalize(1); // => '1st' +ordinalize(2); // => '2nd' +ordinalize(1002); // => '1002nd' +ordinalize(1003); // => '1003rd' +ordinalize(-11); // => '-11th' +ordinalize(-1021); // => '-1021st' ``` - -### Inflector.inflections +### inflections ```js -Inflections inflections([string locale]) -inflections([string locale], [function(Inflections) fn]) +inflections([string locale]) ``` -A singleton instance of the internal Inflections class is yielded by this function, which can then be used to specify additional inflection rules. If passed an optional locale, rules for other languages can be specified. The default locale is "en". Only rules for English are provided by this library. +Get the inflections object for a given locale. The result can be modified to add additional inflection rules. ```js -Inflector.inflections('en', function(inflect) { - inflect.plural(/^(ox)$/i, '$1$2en'); - inflect.singular /^(ox)en/i, '$1'); +import { inflections } from "superflected"; +const inflect = inflections('en') +inflect.plural(/^(ox)$/i, '$1$2en'); +inflect.singular /^(ox)en/i, '$1'); - inflect.irregular('octopus', 'octopi'); +inflect.irregular('octopus', 'octopi'); - inflect.uncountable('equipment', 'snow'); -}); +inflect.uncountable('equipment', 'snow'); ``` New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the pluralization and singularization rules that is run. This guarantees that your rules run before any of the rules that may already have been loaded. - -### Inflector.transliterate +### transliterate ```js -string transliterate(string sentence[, object options]) +transliterate(sentence: string, options?: object): string ``` Replaces non-ASCII characters with an ASCII approximation, or if none exists, a replacement character which defaults to "?". ```js -Inflector.transliterate('Ærøskøbing') // => 'AEroskobing' +transliterate("Ærøskøbing"); // => 'AEroskobing' ``` Default approximations are provided for Western/Latin characters, e.g, "ø", "ñ", "é", "ß", etc. @@ -303,68 +268,63 @@ This method is I18n-aware, so you can set up custom approximations for a locale. In order to make your custom transliterations available, you must set them using the `approximate` helper function: ```js -Inflector.transliterations('de', function(t) { - t.approximate('ü', 'ue'); - t.approximate('ö', 'oe'); -}); +import { transliterations } from "superflected"; +const transliterator = transliterations("de"); +transliterator.approximate("ü", "ue"); +transliterator.approximate("ö", "oe"); ``` Now you can have different transliterations for each locale: ```js -Inflector.transliterate('Jürgen') // => 'Jurgen' -Inflector.transliterate('Jürgen', { locale: 'de' }) // => 'Juergen' +transliterate("Jürgen"); // => 'Jurgen' +transliterate("Jürgen", { locale: "de" }); // => 'Juergen' ``` - -### Inflector.parameterize +### parameterize ```js -string parameterize(string sentence[, object options]) +parameterize(sentence: string, options?: { separator?: string, preserveCase?: boolean }): string ``` Replaces special characters in a string so that it may be used as part of a 'pretty' URL. ```js -Inflector.parameterize('Donald E. Knuth') // => 'donald-e-knuth' -Inflector.parameterize('Donald E. Knuth', { separator: '+' }) // => 'donald+e+knuth' +parameterize("Donald E. Knuth"); // => 'donald-e-knuth' +parameterize("Donald E. Knuth", { separator: "+" }); // => 'donald+e+knuth' ``` -As of v2.1, there's also a `preserveCase` option: +There's a `preserveCase` option: ```js -Inflector.parameterize('Donald E. Knuth', { preserveCase: true }) // => 'Donald-E-Knuth' +parameterize("Donald E. Knuth", { preserveCase: true }); // => 'Donald-E-Knuth' ``` - -### Inflector.constantify +### constantify ```js -string constantify(string words) +constantify(words: string): string ``` Converts words (camelCased, under_scored, or dasherized) to CONSTANT_CASE. ```js -Inflector.constantify('bankAccount') // => 'BANK_ACCOUNT' -Inflector.constantify('bank-account') // => 'BANK_ACCOUNT' -Inflector.constantify('bank_account') // => 'BANK_ACCOUNT' -Inflector.constantify('Bank Account') // => 'BANK_ACCOUNT' +constantify("bankAccount"); // => 'BANK_ACCOUNT' +constantify("bank-account"); // => 'BANK_ACCOUNT' +constantify("bank_account"); // => 'BANK_ACCOUNT' +constantify("Bank Account"); // => 'BANK_ACCOUNT' ``` - - ## Contributing Here's a quick guide: -1. Fork the repo and `make install`. -2. Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate: `make test`. +1. Fork the repo and `pnpm install`. +2. Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate: `pnpm test`. 3. Add a test for your change. Only refactoring and documentation changes require no new tests. If you are adding functionality or are fixing a bug, we need a test! 4. Make the test pass. 5. Push to your fork and submit a pull request. - ## Licence Released under The MIT License.