Getting the first dollar amount using JavaScript with Razor and .Net - javascript

I asked a similar question on how to do this on the server side (SQL), however it makes more sense to accomplish this on the client side, based on the app architecture.
I've got a MVC3 app with Razor on the .Net framework, where I have model data available that I would like to parse and return the first dollar value from a given string using Javascript / regex,
For example, each of the following lines represents a sample data set:
Used knife set for sale $200.00 or best offer.
$4,500 Persian rug for sale.
Today only, $100 rebate.
Five items for sale: $20 Motorola phone car charger, $150 PS2, $50.00 3 foot high shelf.
I've seen a few issues already including the # in JS and a few other pitfalls I would like to try to avoid.
Thanks.

var m = line.match(/\$[0-9,]+\.?\d*/);
if (m)
return m[0];
should give you a hint. This Regex returns you a string which consists of a dollar sign, some numbers or commata, and optional a dot another few numbers behind it. You might want to limit its wideness (only 2 decimals, not starting with zero etc).

Related

Converting between Bases, and from a String to any Base N in JavaScript (Radix conversion)

First post on here!
I've done a couple hours of research, and I can't seem to find any actual answers to this, though it may be my understanding that's wrong.
I want to convert a string, lets say "Hello 123" into any Base N, lets say N = 32 for simplicity.
My Attempt
Using Javascript's built-in methods (Found through other websites, and):
stringToBase(string, base) {
return parseInt(string, 10).toString(base);
}
So, this encodes the string to base 10 (decimal) and then into the base I want, however the caveat with this is that it only works from 2 to 36, which is good, but not really in the range that I'm looking for.
More
I'm aware that I can use the JS BigInt, but I'm looking to convert with bases as high as 65536 that uses an arbitrary character set that does not stop when encountering ASCII or (yes I'm aware it's completely useless, I'm just having some fun and I'm very persistent). Most solutions I've seen use an alphabet string or array (e.g. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-").
I've seen a couple threads that say that encoding to a radix which is not divisible by 2 won't work, is that true? Since base 85, 91, exist.
I know that the methods atob() and btoa() exist, but this is only for Radix/Base 64.
Some links:
I had a look at this github page: https://github.com/gliese1337/base-to-base/blob/main/src/index.ts , but it's in typescript and I'm not even sure what's going on.
This one is in JS: https://github.com/adanilo/base128codec/blob/master/b128image.js . It makes a bit more sense than the last one, but the fact there is a whole github page just for Base 128 sort of implies that they're all unique and may not be easily converted.
This is the aim of the last and final base: https://github.com/qntm/base65536 . The output of "Hello World!" for instance, is "驈ꍬ啯𒁗ꍲ噤".
(I can code java much better than JS, so if there is a java solution, please let me know as well)

algorithm to generate short unique human readable id from first and last name

looking for an algorithm that when given a First and a last name, an id is generated such that it consists of purely alphanumeric characters. Also, I would want this to be as short as possible whilst maintaining uniqueness. I was hoping for around 10-12 characters - something that a human could enter.
I have read about suggestions of computing a hash, then simply taking the first n bytes and calling modulus with 36 (the idea is that you have a mapping from 0-35 to the letters a-z 0-9).
Also heard suggestions of maybe truncating and using a higher base to pack more bits into the id.
I guess I could append some encoding of the generation time to the produced id to make it unique but again I need a way for this to be short.
What's your opinion? Are there specific hashing algorithms/truncating methods I should go for? I'll be implementing it in javascript as part of a static html page used as a local webapp.
I am just worried as crypto is hard and I would welcome advice from anyone who thinks they know what they are doing with it.
If it helps the number of ids I expect to make is small - around 4 digits.
One technique would be to just use a combination of the first name and last name, similar to how large companies create email aliases. If you only are creating a few thousand, it wouldn't be hard to work around collisions. These are probably the most human friendly type of id to deal with. For example, Bill Smith would be billsm or something similar.
If you don't want your ids to be easily guessable (though if guessing an id breaks your security model you should probably look into that) then you can go with something like the following (untested javascript pseudocode):
var sequence = 1,
shardId = 1,
epoch = 1357027200000;
function nextId() {
sequence = sequence + 1;
now = Date.now() - epoch;
seqId = sequence % 1023
nextId = now << 15 | shardId << 10 | seqId;
return (nextId).toString(36);
}

How can I get a massive text file into a JavaScript array?

I'm using a list of words with positive and negative sentiment from AFINN to do some text analysis.
Problem is, the list comes in a .txt file in the following format (word on the left, pos vs neg index at right):
casualty -2
catastrophe -3
catastrophic -4
cautious -1
celebrate 3
celebrated 3
celebrates 3
celebrating 3
To work with it, I need it in the following format:
var array = [{word:"casualty",score:-2},{word:"catastrophe",score:-3},{word:"catastrophic",score:-4}, etc etc]
I'd actually prefer to do this once with a shell script, rather than in the browser. Which is why I'm thinking Node.js could come in handy here. But I'm not very familiar with Node.
Direct link to the zip containing the raw text files.
In case you don't really care about how to read text into a javascript array, and you just need AFINN in JSON, I just found a version here.

Adding commas to large numbers in d3.js

I have data points on a line, when the cursor passes over them, a tooltip div appears with data about that point. The code for the html of the div is below.
div.html("The Avengers Box Office: $" + d.Avengers)
d.Avengers returns 207438708 as a string. So is there any way to add commas to large numbers in d3.js?
I have values ranging from thousands to hundreds of millions and it can be tricky to read them.
Thanks
Solution:
You can't add commas using d3 but you can do via plain JavaScript: Number.toLocaleString()
// convert Avengers to a Number and use the toLocaleString on it
div.html("The Avengers Box Office: $" + (1*d.Avengers).toLocaleString() )
Advanced Solution: If your main problem is that numbers are just too big (even with commas), you need write some really cool conversion functions for your numbers that
count the decimals and
choose a really nice format to display nice numbers
Check out a somehow related answer where I added some code to do that, converting big numbers to "kilo", "mega", "giga", and "terra". You might want to adopt that.
Thanks for the replies guys. I found the answer here. Its not adding commas, but its adding M or K after millions or thousands. Which is actually better I think.
https://groups.google.com/forum/?fromgroups=#!topic/d3-js/YFsSmzu4JZk
d3.format(',')(1999222) //// output=1,999,222

Phone number validation using JavaScript with .NET?

How do I validate phone numbers using JavaScript in ASP.NET?
Validating telephone numbers is a difficult problem.
Essentially, you define a regular expression that match the pattern(s) for a valid phone number. The conventions for a phone number are highly local-specific and without knowing something about the users' location, it's hard to generalize. My general rule is "Take what the user gives you, strip off everything but the digits and store only that, formatting it for display. Even that doesn't always work well, because somebody might give you a perfectly valid number like 555-1234 x345, because they can only be reached via a PBX extension. They've omitted the area code and if you strip off the non-digits, you're left with 5551234345 which would get formatted as 555-123-4335. Not so useful.
NANP (North American Numbering Plan) phone numbers have a 3-digit area code (optional), a 3-digit Central Office (CO/exchange) number and a 4-digit subscriber number, plus optional country code, access code, etc. Conventionally written as (variously, and omitting the country code and access code): (AAA) BBB-CCCC, AAA-BBB-CCCC, AAA.BBB.CCCC, etc. More formally, an NANP number (Zone 1, including the USA and its overseas territories, Candada and most Caribbean countries) should be written as +1:AAA-BBB-CCCC.
The French Numbering Plan currently has 10 digit telephone numbers, written as xx.xx.xx.xx.xx. It used to have 8-digit numbers, with Isle de France (metro Paris) having special rules. More formally, a French number should be written as +33:xx.xx.xx.xx.xx, except that if dialed form outside France, the leading '0' in the telephone number should be omitted, so from an outside perspective, the phone number should be expressed as +33:x.xx.xx.xx.xx.
Other countries and dialing/numbering plans have their own rules. Sometimes there are special rules in places regarding calls placed to and from specific locations in a given country (e.g., it used to be that calling a number located outside Isle de France from Paris required dialing a '16' prefix first.) Here in the US, some locations, require dialing all 10 digits even for local calls.
More info (and links) at the World Telephone Numbering Guide
function validatePhoneNumber(elementValue){
var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
return phoneNumberPattern.test(elementValue);
}
Use it like this:
if (validatePhoneNumber('123-456-7890'))
{
// do something
}

Categories