Skip to content

stringToFilename() Helper Method

The stringToFilename() Helper Method module provides a helper function that takes in a string argument and returns a filename safe variation of the argument string.

Import

To import the stringToFilename() Helper Method:

javascript
import { stringToFilename } from '@obewds/vueventus'

Arguments

Returns: String

ArgsTypeStatusDescription
textStringRequiredString to convert to a file name safe format

Use Example

javascript
console.log( stringToFilename('Test String') )
// returns (string): 'TestString'

console.log( stringToFilename('Test-String _here') )
// returns (string): 'Test-String_here'

console.log( stringToFilename('Test-String! _@here') )
// returns (string): 'Test-String_here'

console.log( stringToFilename('Test.String._@here.jpg') )
// returns (string): 'Test.String._here.jpg'

console.log( stringToFilename('Test. 12345._@here.jpg') )
// returns (string): 'Test.12345._here.jpg'

Module Code

ts
// ./src/helpers/stringToFilename.ts

export default function(string: string): string {

    return string.replace(/[^A-z0-9._-]/gi, '')

}

Released under the MIT License