Coder Social home page Coder Social logo

Comments (6)

floere avatar floere commented on August 18, 2024

Hi angelacode

I need a bit more info to be able to help:

  • Is the input just a string?
    If yes, you can do e.g. one of these three methods:
    "1" << your_string
    or
    "1" + your_string
    or
    "1#{your_string}"
  • Does that input go through phony or does it come from phony and you'd like to add a 1?

Cheers
Florian

from phony.

angelacode avatar angelacode commented on August 18, 2024

Hi, there, he's what I do now, whenever I save a contact, I do something like this:

self.phone = "1"+Phony.normalize(self.phone) unless self.phone.empty?

This works fine if I input, say (404)555-1212.

It then saves that as 14045551212.

But what happens when i edit and update that contact's other fields?

It now tries to normalize 114045551212 -- by adding yet another "1"....!

Is there a way to know whether there's a 1 or not or avoid this? Seems like before I can normalize, I need to add the "1".

from phony.

floere avatar floere commented on August 18, 2024

Hi angelacode

Am I correct in assuming you only work with north american numbers?
According to the NANP: http://en.wikipedia.org/wiki/North_American_Numbering_Plan
the digit after the 1 can only be in the range [2-9].
So a cheap solution would be to add a 1 if there isn't one already.

Some sample code you can run:
a = '14045551212'
b = '4045551212'

a = "1#{a}" if a !~ /\A1/
b = "1#{b}" if b !~ /\A1/

p a
p b

We (at my last company) had a more sophisticated solution. We had two fields in the database, a normalized_number and a number. Whenever a user entered a phone number, we saved it in the number field and tried to normalize it. If that succeeded, we saved the normalized number in the normalized_number field.
We used the number from the normalized_number field for purposes like callto: links and displaying a nicely formatted number using Phony.

Did that help you?

Cheers
Florian

from phony.

angelacode avatar angelacode commented on August 18, 2024

um..let's see, I have it currently add "1" after normalizing through Phony. But what happens if I update the record? It will still add a "1"...the following, for example, properly normalizes 415-555-1212 to 14155551212, but if I edit, it doesn't...

 self.fax = "1"+Phony.normalize(self.fax) unless self.fax.blank? || (self.fax.length == 11 && self.fax[0] == '1')

from phony.

floere avatar floere commented on August 18, 2024

Hi angelacode

Have you maybe got failing specs you can show me?

This issue is starting to move away from Phony, so I'd like to close it. Would that be fine for you?

Concerning your problem:
I do not have all the context of the line you have pasted in the last comment. But…

What I'd do is check if the first number is not a 1, like so:
self.number !~ /\A[^\d]_1/
Then, if yes, add a 1, and normalize the resulting number with Phony, something like this:
self.number = Phony.normalize(add_north_american_country_code_to(self.number)) if self.number !~ /\A[^\d]_1/

I hope that helps, good luck!
Florian

from phony.

floere avatar floere commented on August 18, 2024

I'm closing this one – please reopen if needed.

from phony.

Related Issues (20)

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.