Created
August 15, 2017 20:38
-
-
Save sylwiaeb/c54a08701647614fd12f3727fba5478c to your computer and use it in GitHub Desktop.
[js] extract numbers from text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// converts '7-foo me, 30-boo me, 30-baz me' to '7,30,30' | |
function serialize(text) { | |
return text.replace(/[^\d,]/g, ''); | |
} | |
// converts '7,30,30' to '7-foo me, 30-boo me, 30-baz me' | |
function humanize(text) { | |
const words = [ | |
'-foo', | |
'-boo', | |
'-baz', | |
]; | |
arr = text.split(','); | |
arr.forEach((el, i) => arr[i] = el + words[i]); | |
return arr.join(', '); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment