Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Javascript Version's of Laravel's Illuminate\Support\Str, Illuminate\Support\Stringify, & Illuminate\Support\Fluent

License

NotificationsYou must be signed in to change notification settings

zhorton34/laravel-js-str

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VersionTravisPRs WelcomeCode PenMaintenancedependenciesnpm license

Project Goals


  • Fluent
  • Stringable
  • Simplified


Code Pen Example


Laravel JS Str


"Laravel's Illuminate\Str & Illuminate\Stringify Including Str.of() In Javascript"


Installation


NPM

npm install --save-dev laravel-js-str

Yarn

yarn add laravel-js-str --save

CDN

<scriptsrc='https://unpkg.com/laravel-js-str@latest/build/index.min.js'></script>

Str


Documentation For Each String Method Points To Laravel. Javascript examples are below, Laravel docs will specify what each method specifically does. Replace Str::method() with Str.() when using this package

const{ Str}=require('laravel-js-str');letslice=Str.after('This is my name','This is');// ' my name'
const{ Str}=require('laravel-js-str');letslice=Str.afterLast('App\Http\Controllers\Controller','\\');// 'Controller'
const{ Str}=require('laravel-js-str');letslice=Str.ascii('û');// 'u'
const{ Str}=require('laravel-js-str');letslice=Str.before('This is my name','my name');// 'This is '
const{ Str}=require('laravel-js-str');letslice=Str.beforeLast('This is my name','is');// 'This '
const{ Str}=require('laravel-js-str');letslice=Str.between('This is my name','This','name');// ' is my '
const{ Str}=require('laravel-js-str');letconverted=Str.camel('foo_bar');// fooBar
const{ Str}=require('laravel-js-str');letcontains=Str.contains('This is my name','my');// true
const{ Str}=require('laravel-js-str');letcontains=Str.contains('This is my name',['my','foo']);// true
const{ Str}=require('laravel-js-str');letcontainsAll=Str.containsAll('This is my name',['my','name']);// true
const{ Str}=require('laravel-js-str');letresult=Str.endsWith('This is my name','name');// true
const{ Str}=require('laravel-js-str');letresult=Str.endsWith('This is my name',['name','foo']);// trueresult=Str.endsWith('This is my name',['this','foo']);// false
const{ Str}=require('laravel-js-str');letadjusted=Str.finish('this/string','/');// this/string/adjusted=Str.finish('this/string/','/');// this/string/
const{ Str}=require('laravel-js-str');letmatches=Str.is('foo*','foobar');// truematches=Str.is('baz*','foobar');// false

isAscii is experimental, not confident it works in all scenarios

const{ Str}=require('laravel-js-str');letisAscii=Str.isAscii('Taylor');// trueisAscii=Str.isAscii('ü');// false
const{ Str}=require('laravel-js-str');letisUuid=Str.isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');// trueisUuid=Str.isUuid('laravel');// false
const{ Str}=require('laravel-js-str');letconverted=Str.kebab('fooBar');// foo-bar
const{ Str}=require('laravel-js-str');letlength=Str.length('Laravel');// 7
const{ Str}=require('laravel-js-str');lettruncated=Str.limit('The quick brown fox jumps over the lazy dog',0);// The quick brown fox...
const{ Str}=require('laravel-js-str');lettruncated=Str.limit('The quick brown fox jumps over the lazy dog',0,'(...)');// The quick brown fox (...)
const{ Str}=require('laravel-js-str');letconverted=Str.lower('LARAVEL');// laravel

Plural Package Used

const{ Str}=require('laravel-js-str');letplural=Str.plural('car');// carsplural=Str.plural('child');// children

Plural Package Used

const{ Str}=require('laravel-js-str');letplural=Str.plural('child');// childrenplural=Str.plural('child');// child
const{ Str}=require('laravel-js-str');letrandom=Str.random(40);
const{ Str}=require('laravel-js-str');letstring='The event will take place between ? and ?';letreplaced=Str.replaceArray(['?','8:30','9:00'],string);// The event will take place between 8:30 and 9:00
const{ Str}=require('laravel-js-str');letreplaced=Str.replaceFirst('the','a','the quick brown fox jumps over the lazy dog');// a quick brown fox jumps over the lazy dog
const{ Str}=require('laravel-js-str');letreplaced=Str.replaceLast('the','a','the quick brown fox jumps over the lazy dog');// the quick brown fox jumps over a lazy dog
const{ Str}=require('laravel-js-str');letsingular=Str.singular('cars');// carsingular=Str.singular('children');// child
const{ Str}=require('laravel-js-str');letslug=Str.slug('Laravel 5 Framework','-');// laravel-5-framework
const{ Str}=require('laravel-js-str');letconverted=Str.snake('fooBar');// foo_bar
const{ Str}=require('laravel-js-str');letadjusted=Str.start('this/string','/');// /this/stringadjusted=Str.start('/this/string','/');// /this/string
const{ Str}=require('laravel-js-str');letresult=Str.startsWith('This is my name',This');// true
const{ Str}=require('laravel-js-str');letconverted=Str.studly('foo_bar');// FooBar
const{ Str}=require('laravel-js-str');letconverted=Str.substr('The Laravel Framework',,);// Laravel
const{ Str}=require('laravel-js-str');letconverted=Str.title('a nice title uses the correct case');// A Nice Title Uses The Correct Case
const{ Str}=require('laravel-js-str');letstring=Str.ucfirst('foo bar');// Foo bar
const{ Str}=require('laravel-js-str');letstring=Str.upper('laravel');// LARAVEL
const{ Str}=require('laravel-js-str');returnStr.uuid();
const{ Str}=require('laravel-js-str');Str.words('Perfectly balanced, as all things should be.',3,'>>>');// Perfectly balanced, as >>>
const{ Str}=require('laravel-js-str');letslice=Str.of('This is my name').after('This is');// ' my name'
const{ Str}=require('laravel-js-str');letslice=Str.of('App\Http\Controllers\Controller').afterLast('\\');// 'Controller'
const{ Str}=require('laravel-js-str');letstring=Str.of('Taylor').append(' Otwell');// 'Taylor Otwell'

Experimental Method, not sure this works in all cases

const{ Str}=require('laravel-js-str');letstring=Str.of('ü').ascii();// 'u'
const{ Str}=require('laravel-js-str');letstring=Str.of('/foo/bar/baz').basename();// 'baz'
const{ Str}=require('laravel-js-str');letstring=Str.of('/foo/bar/baz.jpg').basename('.jpg');// 'baz'
const{ Str}=require('laravel-js-str');letslice=Str.of('This is my name').before('my name');// 'This is '
const{ Str}=require('laravel-js-str');letslice=Str.of('This is my name').beforeLast('is');// 'This '
const{ Str}=require('laravel-js-str');letconverted=Str.of('foo_bar').camel();// fooBar
const{ Str}=require('laravel-js-str');letcontains=Str.of('This is my name').contains('my');// true
const{ Str}=require('laravel-js-str');letcontains=Str.of('This is my name').contains(['my',foo']);// true
const{ Str}=require('laravel-js-str');letcontainsAll=Str.of('This is my name').containsAll(['my','name']);// true
const{ Str}=require('laravel-js-str');letstring=Str.of('/foo/bar/baz').dirname();// '/foo/bar'
const{ Str}=require('laravel-js-str');letstring=Str.of('/foo/bar/baz').dirname(2);// '/foo'
const{ Str}=require('laravel-js-str');letresult=Str.of('This is my name').endsWith('name');// true
const{ Str}=require('laravel-js-str');letresult=Str.of('This is my name').endsWith(['name','foo']);// trueresult=Str.of('This is my name').endsWith(['this','foo']);// false
const{ Str}=require('laravel-js-str');letresult=Str.of('Laravel').exactly('Laravel');// true

Collect.js

const{ Str}=require('laravel-js-str');letcollection=Str.of('foo bar baz').explode(' ');// collect(['foo', 'bar', 'baz'])
const{ Str}=require('laravel-js-str');letadjusted=Str.of('this/string').finish('/');// this/string/adjusted=Str.of('this/string/').finish('/');// this/string/
const{ Str}=require('laravel-js-str');letmatches=Str.of('foobar').is('foo*');// truematches=Str.of('foobar').is('baz*');// false

isAscii is Experimental, not positive its correct in all cases

const{ Str}=require('laravel-js-str');letresult=Str.of('Taylor').isAscii();// trueresult=Str.of('ü').isAcii();// false
const{ Str}=require('laravel-js-str');letresult=Str.of('  ').trim().isEmpty();// trueresult=Str.of('Laravel').trim().isEmpty();// false
const{ Str}=require('laravel-js-str');letresult=Str.of('  ').trim().isNotEmpty();// falseresult=Str.of('Laravel').trim().isNotEmpty();// true
const{ Str}=require('laravel-js-str');letconverted=Str.of('fooBar').kebab();// foo-bar
const{ Str}=require('laravel-js-str');letlength=Str.of('Laravel').length();// 7
const{ Str}=require('laravel-js-str');lettruncated=Str.of('The quick brown fox jumps over the lazy dog').limit(20);// The quick brown fox...
const{ Str}=require('laravel-js-str');lettruncated=Str.of('The quick brown fox jumps over the lazy dog').limit(20,' (...)');// The quick brown fox (...)
const{ Str}=require('laravel-js-str');letresult=Str.of('LARAVEL').lower();// 'laravel'
const{ Str}=require('laravel-js-str');letstring=Str.of('  Laravel  ').ltrim();// 'Laravel  'string=Str.of('/Laravel/').ltrim('/');// 'Laravel/'
const{ Str}=require('laravel-js-str');letresult=Str.of('foo bar').match('/bar/');// 'bar'result=Str.of('foo bar').match('/foo (.*)/');// 'bar'

Match All Coming Soon

const { Str } = require('laravel-js-str');let result = Str.of('bar foo bar').matchAll('/bar/');// collect(['bar', 'bar'])
const { Str } = require('laravel-js-str');let result = Str.of('bar fun bar fly').matchAll('/f(\w*)/');// collect(['un', 'ly']);
const{ Str}=require('laravel-js-str');letplural=Str.of('car').plural();// carsplural=Str.of('child').plural();// children
const{ Str}=require('laravel-js-str');letplural=Str.of('child').plural(2);// childrenplural=Str.of('child').plural(1);// child
const{ Str}=require('laravel-js-str');letstring=Str.of('Framework').prepend('Laravel ');// Laravel Framework
const{ Str}=require('laravel-js-str');letreplaced=Str.of('Laravel 6.x').replace('6.x','7.x');// Laravel 7.x
const{ Str}=require('laravel-js-str');letstring='The event will take place between ? and ?';letreplaced=Str.of(string).replaceArray('?',['8:30','9:00']);// The event will take place between 8:30 and 9:00
const{ Str}=require('laravel-js-str');letreplaced=Str.of('the quick brown fox jumps over the lazy dog').replaceFirst('the','a');// a quick brown fox jumps over the lazy dog
const{ Str}=require('laravel-js-str');letreplaced=Str.of('the quick brown fox jumps over the lazy dog').replaceLast('the','a');// the quick brown fox jumps over a lazy dog
const{ Str}=require('laravel-js-str');letreplaced=Str.of('(+1) 501-555-1000').replace('/[^A-Za-z0-9]++/','');//'15015551000'
const{ Str}=require('laravel-js-str');letstring=Str.of('  Laravel  ').rtrim();// '  Laravel'string=Str.of('/Laravel/').rtrim('/');// '/Laravel'
const{ Str}=require('laravel-js-str');letsingular=Str.of('cars').singular();// carsingular=Str.of('children').singular();// child
const{ Str}=require('laravel-js-str');letslug=Str.of('Laravel Framework').slug('-');// laravel-framework
const{ Str}=require('laravel-js-str');letconverted=Str.of('fooBar').snake();// foo_bar
const{ Str}=require('laravel-js-str');letsegments=Str.of('one, two, three').split('/[\s, +/');// collect(["one", "two", "three"])
const{ Str}=require('laravel-js-str');letadjusted=Str.of('this/string').start('/');// /this/stringadjusted=Str.of('/this/string').start('/');// /this/string
const{ Str}=require('laravel-js-str');letresult=Str.of('This is my name').startsWith('This');// true
const{ Str}=require('laravel-js-str');letconverted=Str.of('foo_bar').studly();// FooBar
const{ Str}=require('laravel-js-str');letstring=Str.of('Laravel Framework').substr(8);// Frameworkstring=Str.of('Laravel Framework').substr(8,);// Frame
const{ Str}=require('laravel-js-str');letconverted=Str.of('a nice title uses the correct case').title();// A Nice Title Uses The Correct Case
const{ Str}=require('laravel-js-str');letstring=Str.of('  Laravel  ').trim();// 'Laravel'string=Str.of('/Laravel/').trim('/');// 'Laravel'
const{ Str}=require('laravel-js-str');letstring=Str.of('foo bar').ucfirst();// Foo bar
const{ Str}=require('laravel-js-str');letadjusted=Str.of('laravel').upper();// LARAVEL
const{ Str}=require('laravel-js-str');letstring=Str.of('  ').whenEmpty(function(string){returnstring.trim().prepend('Laravel');});// 'Laravel'
const{ Str}=require('laravel-js-str');letstring=Str.of('Perfectly balanced, as all things should be.').words(3,'>>>');// Perfectly balanced, as >>>

Playground Examples


Curious, but not 100% on whether this is what you're looking for?

The most powerful method is Str.of('example'), allowing us to fluently chain Str methods together

Example

let{ Str}=require('laravel-js-str');lethome='https://planets.com';lettitle='hello mars, a cool world for you to visit, maybe?';letarticle=Str.of(title).replaceFirst(',','').after('hello').before('for you').trim().start('/').finish('/').kebab();letresource=home+article// resource value:// 'https://planets.com/mars-a-cool-world/'//// article value:// Stringable: { value: 'https://planets.com/mars-a-cool-world-to-visit', replace, before, after, etc... }//

Utilization


The most powerful method is Str.of('example'), allowing us to fluently chain Str methods together

Example

let{ Str}=require('laravel-js-str');lethome='https://planets.com';lettitle='hello mars, a cool world for you to visit, maybe?';letarticle=Str.of(title).replaceFirst(',','').after('hello').before('for you').trim().start('/').finish('/').kebab();letresource=home+article// resource value:// 'https://planets.com/mars-a-cool-world/'//// article value:// Stringable: { value: 'https://planets.com/mars-a-cool-world-to-visit', replace, before, after, etc... }//

Contribute


PRs are welcomed to this project.If you want to improve this package, addfunctionality or improve the docs please feel free to submit a PR.


Security Vulnerabilities


If you discover a security vulnerability within Clean Code Studio Packages Or Specifically withinlaravel-js-str, please send an e-mail to Zachary Horton viazak@cleancode.studio. All security vulnerabilities will be promptly addressed.


Change Log



Release 1.0.0


  • Initial Release

Versioning


Semantic Versioning

Code StatusStageRuleExample Version
First releaseNew ProductStart with 1.0.01.0.0
Backward compatible bug fixesPatch ReleaseIncrement the third digit1.0.1
Backward compatible new featuresMinor ReleaseIncrement the middle digit and reset last digit to zero1.1.0
Changes that break backward compatibilityMajor ReleaseIncrement the first digit and reset middle and last digits to zero2.0.0

License


MIT ©Zachary Horton (Clean Code Studio)

About

Javascript Version's of Laravel's Illuminate\Support\Str, Illuminate\Support\Stringify, & Illuminate\Support\Fluent

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp