GithubHelp home page GithubHelp logo

emberjs / ember-inflector Goto Github PK

View Code? Open in Web Editor NEW
106.0 106.0 78.0 4.08 MB

ember-inflector goal is to be rails compatible.

License: MIT License

JavaScript 96.58% HTML 3.40% Handlebars 0.02%

ember-inflector's People

Contributors

bradleypriest avatar cibernox avatar dependabot-preview[bot] avatar dependabot[bot] avatar ember-tomster avatar fivetanley avatar fsmanuel avatar glaukommatos avatar gnclmorais avatar heroiceric avatar karlguillotte avatar kratiahuja avatar krisselden avatar locks avatar mmun avatar nathanhammond avatar nullvoxpopuli avatar oinutter avatar olivia avatar olleolleolle avatar rwjblue avatar samselikoff avatar sandstrom avatar sergeastapov avatar stefanpenner avatar tricknotes avatar turbo87 avatar ursm avatar victornamuso avatar vine77 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ember-inflector's Issues

Irregular Singularize of "Drives"

Not sure if this is the proper place to report, but ran into this issue today:

var inflector = Ember.Inflector.inflector;
inflector.singularize('drives')
// > "drife"

I'm also happy to take a crack at fixing this, but just wanted to ensure this fix actually belongs upstream and not in the app itself

project diverged from ember-data's copy

during ember-data's es6ification this project was forgotten, now it is tricky to propagate changes/fixes from the source project to it being vendored in ember-data

  • es6ification

cc @fivetanely

Irregular camel cased words don't get rules applied.

I've been investigating emberjs/data#1447 and have narrowed the issue down to only applying to camel cased words. It would appear to be due to the lower casing of a word before checking for matching rules.

I have added a unit test to the test suite on my fork of the ember data repository but I don't know if you'd prefer the test to be submitted to that repository or this one?

Issue getting this addon working

Versions on my systems

ember-cli: 0.1.3
Ember: 0.1.7
Watchman: 3.0.0
Mac OS X: 10.10
Node: 0.11.14

I followed the instructions on the first page

npm install --save ember-inflector
ember generate ember-inflector

I then imported both ember-inflector.js, and ember-inflector.js.map to my brocfile, this didn't seem to be the cause of my issues as I was having the same issues before I imported them also

and none of the suggested solutions have been working, I am getting undefined when I do

Ember.Inflector.pluralize("taco");

and ember-inflector can't be resolved in the browser console when trying to import it

import Inflector from 'ember-inflector';
import {singularize, pluralize} from 'ember-inflector';

I tried taking out the node module and bower component then reinstalling just the bower component and just use the Ember.Inflector syntax in my app and still seeing same issues

any advice?

Ember addon support

Would it be possible to get an ember-addon version of this? The other issue for creating a bower package would be helpful too, but an ember-addon would be great!

helpers are not accessible to normal add-on consumers

  • ember-data should no longer auto-include these helpers
    • basically its a bug in this repo that they are included auto-matically
    • i'll release 2.x with that fixed
    • @bmac we should coordinate on another ember-inflector / ember-data release
  • we should offer the helpers as an idiomatic ember-addon normally does. (although as they are non-dasherized, so forcibly included an initializer)

Missing license

This repository is missing a license file. I'm assuming it should be MIT...

Any plan to automatically support underscore and camelcase versions?

I have found that I need to separately declare rules for camelCase and underscore versions of phrases. For example:

inflector = new Ember.Inflector();
inflector.plural(/$/, 's');
inflector.singular(/s$/, '');  
inflector.uncountable('furry_platypus');
inflector.singularize('furry_platypus');       # furry_platypus
inflector.singularize('furryPlatypus');         # furryPlatypu

Particularly given Ember usually gracefully takes care of conversion between camelCase and underscore versions of classes, it seems like it would be helpful not to need to declare both:

inflector.uncountable('furry_platypus');
inflector.uncountable('furryPlatypus');

I haven't checked what rails does, maybe this feature is absent for compatibility with rails?

see if we can figure out source maps for ember-cli

The sourcemap is already been generated, so it should just be an issue of getting broccoli to import it in the right place...

We may also need to update the broccoli config to use a different filepath for sourceURL

Add inflector generator

Currently, if a user wants to add a rule to the inflector, they have no guidance about where to do so. As a result, some people put their rules in the corresponding models, others in an initializer, and only @rwjblue does it correctly.

We're adding documentation to the Guides encouraging @rwjblue's approach, but ideally there would be a generator that would set things up along these lines:

// in app/app.js

// sets up Ember.Inflector
import './models/custom-inflector-rules';
// in app/models/custom-inflector-rules.js
var inflector = Ember.Inflector.inflector;
inflector.irregular('formula', 'formulae');
inflector.uncountable('advice');

Lens model save yields "No model was found for 'len'"

I'm not sure whether this is a bug in the logic for the inflector or within extractUpdateRecord's improper calling of singularize.

Ember.Inflector.inflector.irregular('lens', 'lenses');
Ember.String.singularize('lens'); // "len"

If it's intended for singularize to properly handle already singularized words, I believe the logic for isIrregular to be insufficient. It only checks for irregularInverse for the 'lens' key, but it contains {lenses: 'lens'} and therefore proceeds with standard singularization.

Pluralizing the word 'gas'

Hi!

I've been working on an Ember Project (EmberCLI 1.13.8, Ember 2.0, Ember Data 2.0), and I've created a model 'gas'.

When using ember-data to query the store. this.store.query('gas') the url that's built is /api/gasinstead of what I expected, which would be /api/gases. I've created a custom adapter to deal with the situation, but I think this might be a problem with the ember-inflector.

I've created an Ember Twiddle to show the issue

Any thoughts? Thanks for the project!

"criteria" pluralized should be "criteria"

From my terminal:

โžœ  lol  bundle exec rails c
Running via Spring preloader in process 52353
Loading development environment (Rails 4.2.5.1)
irb(main):001:0> 'criteria'.pluralize
=> "criteria"

Current Ember Inflector pluralizes to criterium.

Pluralize no longer works with the new register-helper method for Handlebars

Helpers registered using Ember.Handlebars.helper receive a number of positional args instead of an array of arguments as we are now expecting. This results in pluralize helper receiving count as its params argument rather than an array of [count, word].

https://github.com/stefanpenner/ember-inflector/blob/5e80bac26f6bc7ff7fc50386f3bee08468c601e8/addon/lib/helpers.js#L43

I have attempted to pass a wrapped version of helperFunction to Ember.Handlebars.helper but haven't been able to get this working as ember-cli seems to be pushing two lots of ember-inflector into my compiled files, one of which I can't seem to edit.

screenshot 2015-03-26 18 44 59

Regression for pluralization of 'price' in v1.9.0

This morning, I attempted to upgrade my app to ember-data v1.13.9 which, in turn, upgraded ember-inflector to v1.9.0. This caused one of my model async relationships to fail, because price was incorrectly pluralized to price.

Previously, using ember-data v1.13.8 and ember-inflector v1.7.0, price was correctly pluralized to prices.

The symptom is that, when my model attempts to look up its associated prices via the DS.ActiveModelAdapter, it looks for GET /api/price/:id instead of GET /api/prices/:id.

To reproduce the bug, simply open a shell / console with Ember imported:

// ember-inflector 1.7.0
inflector.pluralize('price');
// => 'prices'

// ember-inflector 1.9.0
inflector.pluralize('price');
// => 'price'

I attempted to workaround the issue, by adding my own inflection rule to an initializer...

Ember.Inflector.inflector.irregular('price', 'prices');

...but the incorrect singular /price URL was still being requested.

Singularizes 'meta' as 'metum'

Of the things I expected to happen, this was not one of them. Not sure whether rails does the same thing? Does not seem very helpful

Unexpected reserved word (loader problems?)

When upgrading to the latest version I get the following error. I think it might be to do with the loader like in #40.

import Ember from 'ember';
^^^^^^
Unexpected reserved word
/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-inflector/addon/index.js:3
import Ember from 'ember';
^^^^^^
SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Function.Addon.lookup (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/models/addon.js:671:19)
    at /Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/models/addons-factory.js:43:36
    at visit (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/utilities/DAG.js:23:3)
    at DAG.topsort (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/utilities/DAG.js:82:7)
    at AddonsFactory.initializeAddons (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/models/addons-factory.js:40:9)
    at Project.initializeAddons (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/models/project.js:313:36)
    at Project.eachAddonCommand (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/models/project.js:364:10)
    at module.exports (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/cli/lookup-command.js:46:13)
    at CLI.<anonymous> (/Users/jon/Documents/Devon/occam/frontend/node_modules/ember-cli/lib/cli/cli.js:31:26)```

Irregular rule for unitOfMeasure to unitsOfMeasure does not work

I tried adding

inflector.irregular('unitOfMeasure', 'unitsOfMeasure');

To my app and it doesn't work, it hits /api/unitOfMeasure, it should now be hitting /api/unitsOfMeasure

I created a unit test in /tests/unit/inflector-test.js that fails on both asserts in latest master:

test('unit of measure', function(assert) {
  inflector.irregular('unitOfMeasure', 'unitsOfMeasure');

  assert.equal(inflector.singularize('unitsOfMeasure'), 'unitOfMeasure', 'irregular singularization rule was applied');
  assert.equal(inflector.pluralize('unitOfMeasure'), 'unitsOfMeasure', 'irregular pluralization rule was applied');
});

I had a quick debug, it seems to find the irregular rule ok and populate substitution in the for loop, but then I think the return in that loop of return word.replace(rule, substitution); doesn't do the job as word is unitOfMeasure and rule is unitofmeasure, so there is no match due to the casing?

Not working with latest `loader.js`

Because of the project folder structure, the transpiled amd output will not work with the current loader.js#master or the current ember/loader version.

The workable loader must implement:

    if (nameParts.length === 1) {
      parentBase = nameParts;
    } else {
      parentBase = nameParts.slice(0, -1);
    }

But this change does not work for other ember dependencies as {backburner,rsvp,router.js,router-recognizer}.

support dasherized multi-word names?

When using ember-cli, it seems like it would be helpful to support the same inflection patterns for dasherized and undasherized names. Instead, I find myself doing the following:

Ember.Inflector.inflector.uncountable('phrase-tones')
Ember.Inflector.inflector.uncountable('phraseTones')

Of course, if I always wanted anything ending in tones to be uncountable, then I could just register the last word, but sometimes it's only class names that I want to be uncountable (i.e., in the case where each record represents a plural set of things).

Does the inflector know about word boundaries as presented in camelCase? This is a little tricky because of all-caps acronyms. Perhaps it could work as follows. If one registers the phrase-tones case then it also registers the phraseTones case, but not viceversa.

two minor issues with ember-cli-addon

  1. the bower package includes a source map line that will cause a browser fetch that fails. Ember-cli is not going to be serving up this file, not sure what this would do in production yet.

//# sourceMappingURL=ember-inflector.js.map

  1. the directory structure under node_modules/ember-inflector contains a lol directory that mirrors the bower_ember folder - I'm not sure if either folders are needed for the npm install, those folders are not in the github repo.

pluralize handlebars helper

could be as easy as:

Ember.Handlebars.helper('pluralize', function(count, singular) {
  var word = count == 1 ? singular : Ember.String.pluralize(singular);
  return count + ' ' + word;
});

defeatureify / prod builds

Need this in the brocfile for people who use this standalone. Ember data should do it once my pull request there makes it in.

The Word "Podium"

Podium is singular but singularizes to podia (because of words like bacteria -> bacterium I presume).

`singularize()` incorrectly lops off the beginning of dasherized, irregularly pluralized words

This is a problem when you have a model named something like "in-common-person", where in place of "person", any member of the irregularPairs list is at the end of the dasherized list.

To repro:

Ember.String.singularize('in-common-person') // 'common-person'
Ember.String.singularize('in-common-man') // 'common-man'
//etc.

Because of this, any model named in this way is improperly referenced.

CC: @nathanhammond @chadhietala

Dist no longer created

After pulling the latest from master, running bundle exec rake dist no longer creates a distribution. Haven't had time to dig any deeper.

No support for camelCased uncountable rules.

JSFiddle demo of the issue

I can use Ember.Inflector.inflector.uncountable to add a word such as 'criteria' so that Ember's Inflector knows 'criteria' is both the plural and singular form. After I do so, Ember.String.singularize('criteria') returns 'criteria'.

However, I cannot do the same for a word that is camel-cased, such as 'myCriteria'. When I attempt to do so, Ember.String.singularize('myCriteria') still returns 'myCriterium'.

index.hbs:

<dl>
    <dt>Singular</dt>
    <dd>{{singular}}</dd>
    <dt>Plural</dt>
    <dd>{{plural}}</dd>
    <dt>Singular Camelized</dt>
    <dd>{{singularCamel}}</dd>
    <dt>Plural Camelized</dt>
    <dd>{{pluralCamel}}</dd>
</dl>
<button {{action 'fix'}} {{bind-attr disabled=fixed}}>
    Add uncountable rules
</button>

app.js:

App = Ember.Application.create();

App.IndexController = Ember.Controller.extend({
    plural: 'criteria',
    pluralCamel: 'myCriteria',
    fixed: false,
    singular: function() {
        return this.get('plural').singularize();
    }.property('plural', 'fixed'),
    singularCamel: function() {
        return this.get('pluralCamel').singularize();
    }.property('plural', 'fixed'),
    actions: {
        fix: function() {
            Ember.Inflector.inflector.uncountable('criteria');
            Ember.Inflector.inflector.uncountable('myCriteria');
            this.set('fixed', true);
        }
    }
});

Re-submitted here per request from emberjs/data#1554.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.