1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 08:14:06 +02:00

Update readme with todo items, rename truncate to safeTruncate, and clean to collapseWhitespace

This commit is contained in:
Daniel St. Jules
2013-07-24 00:42:53 -04:00
parent e8664d3bde
commit 26ee99743c
3 changed files with 67 additions and 21 deletions

View File

@@ -2,6 +2,8 @@
A PHP library with a variety of string manipulation functions with multibyte support. Inspired by underscore.string.js.
Note: The methods listed below are subject to change until we reach a 1.0.0 release.
* [Requiring/Loading](#requiringloading)
* [Methods](#methods)
* [upperCaseFirst](#uppercasefirst)
@@ -14,7 +16,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [titleize](#titleize)
* [humanize](#humanize)
* [tidy](#tidy)
* [clean](#clean)
* [collapseWhitespace](#collapsewhitespace)
* [standardize](#standardize)
* [pad](#pad)
* [padLeft](#padleft)
@@ -28,7 +30,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [contains](#contains)
* [surround](#surround)
* [insert](#insert)
* [truncate](#truncate)
* [safeTruncate](#safetruncate)
* [reverse](#reverse)
* [shuffle](#shuffle)
* [Tests](#tests)
@@ -183,15 +185,15 @@ Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
S::tidy('“I see…”'); // '"I see..."'
```
##### clean
##### collapseWhitespace
S::clean(string $str)
S::collapseWhitespace(string $str)
Trims the string and replaces consecutive whitespace characters with a
single space.
single space. This inclues tabs and newline characters.
```php
S::clean(' Ο συγγραφέας '); // 'Ο συγγραφέας'
S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγραφέας'
```
##### standardize
@@ -340,9 +342,9 @@ Inserts $substring into $str at the $index provided.
S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
```
##### truncate
##### safeTruncate
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
S::safeTruncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
Truncates the string to a given length, while ensuring that it does not
chop words. If $substring is provided, and truncating occurs, the string
@@ -350,7 +352,7 @@ is further truncated so that the substring may be appended without
exceeding the desired length.
```php
S::truncate('What are your plans today?', 22, '...'); // 'What are your plans...'
S::safeTruncate('What are your plans today?', 22, '...'); // 'What are your plans...'
```
##### reverse
@@ -386,6 +388,50 @@ S::shuffle('fòô bàř', 'UTF-8') // 'àôřb òf'
**isMultibyte**
**wordWrap**
**chars** $callback
**words** $callback
**paragraphs**
**lines**
**excerpt** ($str, $substring, $radius)
**truncate**
**pluralize** ($count, $singular, $plural = null)
**at** $position
**first**
**last**
**from**
**to**
**toBoolean**
**ensureLeft**
**ensureRight**
**isAlpha**
**isAlphaNumeric**
**isUpper**
**isLower**
**isBlank**
**length**
## Tests
[![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy)

View File

@@ -217,12 +217,12 @@ class Stringy
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space.
* single space. This inclues tabs and newline characters.
*
* @param string $str The string to cleanup whitespace
* @return string The trimmed string with condensed whitespace
*/
public static function clean($str)
public static function collapseWhitespace($str)
{
return preg_replace('/\s+/u', ' ', trim($str));
}
@@ -482,7 +482,7 @@ class Stringy
public static function slugify($str)
{
$str = preg_replace('/[^a-zA-Z\d -]/u', '', self::standardize($str));
$str = self::clean($str);
$str = self::collapseWhitespace($str);
return str_replace(' ', '-', strtolower($str));
}
@@ -551,7 +551,7 @@ class Stringy
* @param string $encoding The character encoding
* @return string The resulting string after truncating
*/
public static function truncate($str, $length, $substring = '',
public static function safeTruncate($str, $length, $substring = '',
$encoding = null)
{
$encoding = $encoding ?: mb_internal_encoding();

View File

@@ -263,15 +263,15 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
}
/**
* @dataProvider stringsForClean
* @dataProvider stringsForCollapseWhitespace
*/
public function testClean($expected, $string)
public function testCollapseWhitespace($expected, $string)
{
$result = S::clean($string);
$result = S::collapseWhitespace($string);
$this->assertEquals($expected, $result);
}
public function stringsForClean()
public function stringsForCollapseWhitespace()
{
$testData = array(
array('foo bar', ' foo bar '),
@@ -624,16 +624,16 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
}
/**
* @dataProvider stringsForTruncate
* @dataProvider stringsForSafeTruncate
*/
public function testTruncate($expected, $string, $length, $substring = '',
public function testSafeTruncate($expected, $string, $length, $substring = '',
$encoding = null)
{
$result = S::truncate($string, $length, $substring, $encoding);
$result = S::safeTruncate($string, $length, $substring, $encoding);
$this->assertEquals($expected, $result);
}
public function stringsForTruncate()
public function stringsForSafeTruncate()
{
$testData = array(
array('Test foo bar', 'Test foo bar', 12),