panzerdp / voca Goto Github PK
View Code? Open in Web Editor NEWThe ultimate JavaScript string library
Home Page: https://vocajs.pages.dev
License: MIT License
The ultimate JavaScript string library
Home Page: https://vocajs.pages.dev
License: MIT License
Welcome to Voca's GitHub repo!
in document
https://github.com/panzerdp/voca/blob/master/src/manipulate/pad_left.js
https://vocajs.com/#padLeft
shows:
v.padLeft('bird', 6, '-');
// => '--bird-'
v.padLeft('bird', 6, '-');
resrult:
"--bird"
Browser/OS type: firefox/chrome
The document should be the mistake by my understand.
Individual functions import with ECMAScript modules work in browsers.
Global import using default export for voca is working:
import voca from 'voca';
However, individual function imports with ESM modules is not working:
Uncaught SyntaxError: The requested module '../node_modules/voca/starts_with.js' does not provide an export named 'default'
import startsWith from 'voca/starts_with';
Browser/OS type: Chrome 67
VocaJS version: 1.4.0
Hey panzerdp,
Thanks for this library! I looked through your tests for format
. It looks like it is using the old c % format syntax. Is there plan on supporting the new {:03d}
kind of string format?
Here is a helpful link for some of the things that are possible:
https://pyformat.info | using string.format() for great good
At the moment v.latinise
function (and its dependencies) uses a big list of characters that are latinised.
To decrease the bundle size, support only the most used Unicode blocks:
I am unsure if this is behavior is intended or not but if there is a number that is part of word or acronym it is broken out as a separate word.
Expected behavior:
v.words('What Is The Best MACD Indicator for MT4?')
(8) ["What", "Is", "The", "Best", "MACD", "Indicator", "for", "MT4"]
v.countWords('What Is The Best MACD Indicator for MT4?')
8
Current behavior:
v.words('What Is The Best MACD Indicator for MT4?')
(9) ["What", "Is", "The", "Best", "MACD", "Indicator", "for", "MT", "4"]
v.countWords('What Is The Best MACD Indicator for MT4?')
9
I realize I could come up with my own regex to solve this issue but the default regex works really well besides this one scenario which doesn't seem intended.
I am using Voca 1.4.0
Hi. I think Voca is very good library for string control.
But, Voca is not support to IE8 currently.
I want to know if you will support IE8.
Maybe it is worth adding toString function?
Convert a string to title case.
v.titleCase('hello world')
// => 'Hello World
v.titleCase('Hello world')
// => 'Hello World
v.titleCase('hello World')
// => 'Hello World
This SO question has many suggestions.
The top answer uses string.replace() with regex:
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
Not sure about the coverage of all cases.
In formal title case, words like 'a', 'it', 'of', etc. are ignored (not capitalized). Consider this as an option.
v.titleCase(subject, allWords)
v.titleCase('this is a title of great proportions', true);
// => 'This Is A Title Of Great Proportions'
v.titleCase('this is a title of great proportions', false);
// => 'This is a Title of Great Proportions'
Just a note for your v1.4.1 release notes - I had to make the following change:
// v1.4.0
window.v = require('voca');
// v1.4.1
window.v = require('voca').default;
Welcome to Voca's GitHub repo!
When a codebase has a significant change in GitHub, there should be something published to NPM/Yarn so people can take advantage of those updates through their package manager.
There hasn't been a publishing of this library to NPM/Yarn in over 2 years.
https://www.npmjs.com/package/voca?activeTab=versions
https://yarnpkg.com/package/voca
Not relevant. I was really looking at this as a serious solution for cutting accent characters at my work, but I'm concerned of this not being published to any package management tools in so long. Awesome work on this library, either way. There's some very useful methods here that I would love to use.
Scripts are cool for frontend.
But serverside needs performance.
Chaining the string methods practically causes reloops per chain.
How about synthesizing methods together for one loop?
Starting the project in a few a days.
For e.g.
If the input is "wxyz" then the output of the string should be
['w','wx','wxy','wxyz','wxz','wy','wyz','wz','x','xy','xyz','xz','y','yz','z']
Awesome library, would have a suggestion.
v.stripHtml(htmlTagArray = [''])
If any htmlTag is given within the array the strip function is limited to them otherwise the function removes all html tags.
v.sprintf()
function works for many template requirements, but in some situations might be too heavy and complicated.
A simpler alternative v.template()
function can have a lighter API, and work with arrays and objects.
[API in progress...]
Welcome to Voca's GitHub repo!
When title casing an abbreviation, I would expect the abbreviation to be preserved.
voca.titleCase("HTML is Awesome") // => "HTML is Awesome!"
voca.titleCase("JS is Cool") // => "JS is Cool"
I expect Voca's titleCase
function to respect all title rules. Maybe this warrants a separate function or an additional option?
Instead, Voca converts these words
voca.titleCase("HTML is Awesome") // => "Html is Awesome!"
voca.titleCase("JS is Cool") // => "Js is Cool"
Call the functions as described above.
Browser/OS type: macOS 10.15.3
Node version: 13
On https://vocajs.com/, there is this example :
slugify('café latté'); // => 'caffe-latte'
Should be 'cafe-latte' right ?
Hi,
First, Great work on this library!
Is there any intention to add diacritics which are a useful string manipulation for us Europeans ?
http://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
Cheers !
Hello!
What do you think about idea of implementing such formatting?
If we would name this function format, then here are some examples of how it would work:
v.sformat('{}, {}, {}', 0, 1, 2); => '0, 1, 2'
v.sformat('{0}, {0}, {1}, {2}, {3}', 1, 2, 3, 5); => '1, 1, 2, 3, 5'
v.format('{name}: {a}+{b}={c}', {name: 'sum', a: 1, b: 2, c:3}); => 'sum: 1+2=3'
There would be also possibility to pass function to format.
If you think this is a good idea, then I would implement it in the coming days 😄
Hi,
Great lib! Is there not a pascal case function? Didn't see it in docs or issues but I'm surprised. I assume it could easily be done like this:
v.chain('my thing').camelCase().capitalize().value() // -> 'MyThing'
Would a PR be accepted for this?
Hi! In this code https://github.com/panzerdp/voca/blob/master/src/helper/number/nan_default.js#L11 maybe it meant:
export default function nanDefault(value, defaultValue) {
return +value !== value ? defaultValue : value;
}
Thank you for your work!
Voca library needs a custom build tool to generate configured builds.
Rough the command npm run build
should implement the following settings:
--functions sprintf,camelCase,insert,latinise
--categories escape,format
--exclude-functions insert,latinise
cjs
(CommonJS), umd
(Universal Module Definition), iife
, es
,--format cjs
--minify
--fp
--output dist/voca.cust.build.js
For instance the following command creates a fp build exported as CommonJS of sprintf()
and latinise()
functions:
npm run build --functions sprintf,latinise --format cjs --fp
Hi,
The Title Case implementation doesn't follow proper rules for this case. In the original feature-request #21 these rules where explained but it seems somehow they didn't go into the implementation.
A description of those rules can be seen on:
https://www.bkacontent.com/how-to-correctly-use-apa-style-title-case/
Here is an example from New York Times. (More in the fiddle)
From Obama and Baker, a Lament for a Lost Consensus
From Obama And Baker, A Lament For A Lost Consensus
I've created this fiddle:
https://jsfiddle.net/Qcho86/h97y0vfq/
Here is a reference implementation by John Resig, the creator of jQuery.
https://johnresig.com/blog/title-capitalization-in-javascript/
https://johnresig.com/files/titleCaps.js
As you can see in the fiddle Resig's implementation is not perfect either.
From that page a list of edge cases to take into account:
https://daringfireball.net/projects/titlecase/examples-edge-cases
Hi! I tried the voca.js on explorer v11 and it crashed.
I'm not an expert in javascript but I managed to make it work by adding a few lines. Maybe it could be useful to others?
Modifies:
At the very beginning of the file:
//object.keys support
if (!Object.keys) {
Object.keys = function(obj) {
var keys = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
keys.push(i);
}
}
return keys;
};
}
//object.values support
if (!Object.values) {
Object.values = function(obj) {
var res = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
res.push(obj[i]);
}
}
return res;
};
}
//Array.foreach support
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fn, scope) {
for(var i = 0, len = this.length; i < len; ++i) {
fn.call(scope, this[i], i, this);
}
}
}
//Array.isArray support
if (!Array.isArray) {
Array.isArray = function(item) {
if(item instanceof Array){return true;}
return false;
}
}
//bind support
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
(function (global, factory) {
[...]
And then the function isNil
function isNil(value) {
return value === undefined || value === null || value === '';
}
Now it works.
v.titleCase("professor's office")
'Professor\'s Office'
v.titleCase("professor's office")
'Professor\'S Office'
Node version: 8.1.4
Are there situations that I'm missing here where a letter after a prime/apostrophe should be capitalized?
The same behaviour occurs with real/smart quotes (’
as opposed to '
) as well
> v.titleCase("professor’s office")
'Professor’S Office'
Welcome to Voca's GitHub repo!
expect(v.prune('YATTA, ', 6, '')).toEqual('YATTA,');
expect(v.prune('YATTA, ', 6, '')).toEqual('YATTA'); // the string is missing the `,` at the end
https://github.com/mattiaerre/china-musk/blob/master/src/store/prune.test.js
Browser/OS type: macOS Mojave
Node version: v10
thanks so much,
-Mattia
const image_name = v.rsplit('my.cool.image.png', '.', 1)[0]
console.log(image_name) // "my.cool.image"
See Python docs: https://python-reference.readthedocs.io/en/latest/docs/str/rsplit.html
hey @panzerdp,
In documentation, the first test of swapeCase output should be "lEAGUE OF sHADOWS".
You forgot the letter U
Welcome to Voca's GitHub repo!
v.graphemes("కృష్ణ") should return (2) ["కృ", "ష్ణ"]
v.countGraphemes("కృష్ణ") should return 2
v.graphemes("కృష్ణ") returns (5) ["క", "ృ", "ష", "్", "ణ"]
v.countGraphemes("కృష్ణ") returns 5
v.graphemes("కృష్ణ")
v.countGraphemes("కృష్ణ")
Browser/OS type: n/a
Node version: n/a
I like Voca better than the alternatives, but I can't see how you would convert camelcase into words. In other libs it's called .humanize
For example:
"camelCase" -> "camel case"
Converting the logical booelan strings to respective booleans
v.boolean('true');
// => true
v.boolean('1');
// => true
v.boolean('yes');
// => true
v.boolean('false');
// => false
v.boolean('0');
// => false
v.boolean('no');
// => false
The comparisons are case insensitive
The list of truth values we can consider -> 'true', true, '1', 1, 'yes', 'on'
If this is fine I will submit a pull request for this function implementation.
Welcome to Voca's GitHub repo!
Importing the library should not throw an exception.
Following exception is thrown:
global is not defined
Error in function getGlobalObject in ./node_modules/voca/index.js:3595
3593 | if (typeof global === 'object' && global.Object === Object) {
3594 | // NodeJS global object
> 3595 | globalObject$1 = global;
This seems to be an issue using voca in the latest major update of Gatsby (v3), or perhaps Webpack?
From what I can gather, voca is initialising the global object using "global" (ie NodeJS instance) even though this is a browser instance, I don't understand why it would be entering that if statement
Browser/OS type: Chrome/ Ubuntu 20
Node version: v12.21.0
Hi, congrats with 1.0. First small feedback. When I was browsing Count section I expected to see function to count words which is often used, for example, to generate approx "time read" time.
P.S. could you provide simple plan with some cool links to get started with modern iOS dev and Swift 3 for someone with a huge dev experience in general already? How do you handle env, config, styles, layout, package management? I don't like writing a lot of small UI classes and drag'n'dropin' UI with mouse, if there are packages to use something close to CSS/HTML would be great. May be you can follow me back on https://twitter.com/mevrael so I could DM you : )
Just upgraded my Angular2+ (5) app to latest packages and my voca (which I didn't upgrade) includes call stopped being recognised. I needed to change import v from 'voca' to import * as v from 'voca'.
Might be worth investigating/updating your docs.
Thanks for the brilliant library.
When camelCase with special characters, the characters will be removed.
But when I camelCase with a number, the number will not be removed and the letter next to the number will be capitalized. I am not sure the behave is correct or not.
version: 1.0.0
example:
v.camelCase("BIRD1FLIGHT")
//=> "bird1Flight"
v.camelCase("bird-1flight")
//=>"bird1Flight"
v.camelCase("bird-1flight-111-1--bi4rd 1flight")
//"bird1Flight1111Bi4Rd1Flight"
I think this library should include functions to convert String
s to TypedArray
s and viceversa, with the option to interpret as UTF-8 or UTF-16 depending on use case. So if a string is to be interpreted as UTF-8, the corresponding TypedArray
should be a Uint8Array
, otherwise UTF-16 is used and a Uint16Array
of code-units is returned. If an arbitrary TypedArray
is provided, it'll be read as a string of octets, and those octets will be converted to a string depending on the chosen encoding. UTF-16 has endianess and BOMs, so a function that handles implicit and explicit endianess would be slightly more complicated.
Another thing related to encodings are binary-to-text encodings like Hexadecimal, base64, base85, etc. JS already has base64 support with atob
and btoa
, but hex and base85 are missing, which could be provided by this library.
I don't know if these features should be added to this library because Voca seems to be intended for high-level (not low-level) use cases, and adding base85 support would be pointless because it's rarely used. Any constructive criticism is appreciated
The regexp that matches words works correct for most of the cases. However the regexp is too heavy because it enumerates a lot of unicode character code points.
A lighter alternative is necessary to implement.
This is quite a big suggestion, but in my opinion the order of the arguments is inconsistent across functions, and generally the wrong way around.
See these for details:
http://functionaltalks.org/2013/05/27/brian-lonsdorf-hey-underscore-youre-doing-it-wrong/
https://jsleao.wordpress.com/2015/02/22/curry-and-compose-why-you-should-be-using-something-like-ramda-in-your-code/
There are some cases where API is the right way around, and would allow for currying or partial application:
v.sprintf('%s costs $%.2f', 'Tea', 1.5);
Here, you take config first, data last, which is the "right way". This allows you to things like:
const logMyStuff = v.sprintf.bind(null, "%s costs $%.2f")
logMyStuff("Tea", 1.5)
logMyStuff("Coffee", 2.40)
which allows you to use this function in compose chains, for example.
You cannot do that with this function:
v.replaceAll('good morning', 'o', '*');
If the order were reversed, and currying implemented, you could do this:
const changeOsToStars = v.replaceAll("o", "*")
And now you have a function you can use over and over.
With the use of FP (and in particular partial application and currying) on the rise in JavaScript, I think this library should take a leaf from ramda's book and put config first, data last, and curry by default.
Thoughts?
When title casing an abbreviation, Voca capitalizes some words it shouldn't, like to
or a
. I realize this can get pretty complicated or can vary between styles, but it seems to me that it still be helpful to have a function that followed the general rules.
voca.titleCase("bruce willis is a badass") // => "Bruce Willis is a Badass"
Voca capitalizes those words.
voca.titleCase("bruce willis is a badass") // => 'Bruce Willis Is A Badass'
Call the functions as described above.
Browser/OS type: macOS 10.15.3
Node version: 13
Hi, this is a feature suggestion to this awesome library.
The suggested functions are as follows:
*optional arguments
These are inspired by a program called EasyMorph. And for more info I'll link to their documentation.
This function returns characters after the first occurrence of after_string in text, if found. If after_string is not found within text and "empty" value is returned.
keepAfter('a/b/c/d', 'b') //Returns '/c/d'
This function returns characters before the first occurrence of before_string in text, if found. If before_string is not found within text and "empty" value is returned.
keepBefore('5pm', 'pm') //Returns '5'
This function returns the characters between the first occurrence of keep_start and the first occurrence of keep_end in text.
keepBetween('Mary Joe Smith', 'Mary ', ' Smith') //Returns 'Joe'
This function keeps only the characters given in keep_string and removes the others from text.
keepChars('a1b2c3', 'abc') //Returns 'abc'
Hey there! I've been using voca heavily in an app, however I had to create a plural function for a few places that voca couldn't help with. Since it's a simple helper and seemed to fit in with the other manipulation methods like capitalise() I thought it might be worthy addition to the library.
Pluralises strings, given a subject and an amount.
Also takes an optional plural string for deviations/uncommon plurals.
v.pluralise('dog', 0);
// => 'dogs'
v.pluralise('dog', 1);
// => 'dog'
v.pluralise('dog', 2);
// => 'dogs'
v.pluralise('dog', -4);
// => 'dogs'
v.pluralise('wolf', 1, 'wolves');
// => 'wolf'
v.pluralise('wolf', 42, 'wolves');
// => 'wolves'
Can view the changes here if you'd like me to make a proper PR.
https://github.com/DJTB/voca/commit/0ee27311feca765711cb30bc008d29032cc44829
Are there correspondents of underscore.string
's
strRight()
strRightBack()
strLeft()
strLeftBack()
in voca
?
Hi panzerdp,
Thank for such good utility.
I made a Demo page of Voca APIs, Please have a look: https://caoglish.github.io/Vocademo/
I have not made parameters changing available right now, but I plan to.
Is there a definition file for this library for typescript ?
Welcome to Voca's GitHub repo!
(Please describe here the expected behavior)
(Please describe here the actual behavior)
(Please describe a scenario how to reproduce)
Browser/OS type:
Node version:
Hello, I want to migrate from String.js older (but very important code) used tu sanitize string in our apps but I can't recreate the same usage with voca.
Actually I use this with string.js :
S(text).trim()
.latinise()
.humanize()
.stripPunctuation()
.underscore().s
And this is the result I want for this strings :
original | sanitized |
---|---|
Volume chaudière & appoint | volume_chaudiere_appoint |
test___multi__underscore | test_multi_underscore |
Volume chaudière & appoint 12 | volume_chaudiere_appoint_12 |
A 11yn | a_11yn |
Well k1 | well_k1 |
test&12 | test12 |
a & b | a_b |
a&b | ab |
This is really important to keep this sanitized format for us. Without it, all our application is broken for existing data.
Actually, I converted this function doing this :
snakeCase(latinise(text))
But I have problem with text with attached numeric and alphanumeric characters, or with special char between them :
original | sanitized |
---|---|
A 11yn | a_11_yn |
Well k1 | well_k_1 |
test&12 | test_12 |
a&b | a_b |
I test many functions and I can't find any voca function to do our sanitized part
Pass the original texts into snakeCase(latinise(text))
Browser/OS type: directly in node 18
Node version: latest node 18 LTS
Documentation example shows:
v.lowerCase('BLUE');
// => 'BLUE'
which should be
v.lowerCase('BLUE');
// => 'blue'
I am about to submit a pull request for this fix, albeit quite small. In the future, for small documentation changes such as these, do you prefer just an issue or do you welcome pull requests?
Thanks!
Hi there!
Would you be interested in a function that matches two strings while ignoring diacritics and non-alphanumeric characters? Something similar to https://github.com/dar5hak/equalsish?
v.fuzzyCompare("Some String", "somestring") === true
If so, I would be glad to send a pull request.
It is necessary to implement reverse(subject) function.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.