esamattis / esamattis/underscore.string

Trim Algorithm Is Overly Simplistic

Aperta
#151 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
3.4k
Fork
367
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

If you try doing `_.trim('someProperty', 'Property')`, you might be surprised at the result: instead of "some" (as you might expect), you get "som".

This happens because the current trim implementation just takes every character in your second arg and throws them inside "[]" in a regex. So if a letter in the second arg (eg. 'e') exists in the part that shouldn't be trimmed, that letter (or letters) get trimmed also. In other words, `_.trim(anyStringWhatsoever, 'a-zA-Z1-0') == ''`.

I think a much less error-prone approach would be to check whether arg #1 `startsWith`/`endsWith` arg #2(or both, depending on whether this is a `trim`/`ltrim`/`rtrim`); if not, just return arg #1. If it does start/end with arg #2, simply use `.substring` to do the trimming:
`ltrim = arg1.substring(arg2.length);`
`rtrim = arg1.substring(0, arg1.length - arg2.length);`

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Individua i punti di ingresso di trim, ltrim e rtrim e leggi la loro implementazione attuale e i test; l'issue non indica file specifici. Riproduci prima _.trim('someProperty', 'Property') e i casi correlati. Il lavoro è completato quando il comportamento documentato per prefissi/suffissi è coperto dai test senza rimuovere caratteri non correlati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.