1.0.3 • Published 4 years ago

file-easy v1.0.3

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

file-easy

File utilities to speed up creating document files, setting default extension, and getting a slug from a string.

Installation

npm i file-easy

Usage

.slug()

const fileEasy = require('file-easy')

// getting a slug
let fn = 'source Filename';
let slug = fileEasy.slug(fn)
console.log('Slug:', slug)

Will show:

Slug: source-filename
const fileEasy = require('file-easy')

let names = [
    'Simple_File$Goes%Here',
    '%%Welcome**    Buddy%&^#$%'
];
names.forEach((name) => {
    console.log('Source: "', name, '" is:', fileEasy.slug(name))
})

Will show:

Source: " Simple_File$Goes%Here " is: " simple-file-goes-here
Source: " %%Welcome**    Buddy%&^#$% " is: " welcome-buddy

.setDefaultExtension()

const fileEasy = require('file-easy')

// f1 is filename.js (no extension in original, apply extension)
let f1 = fileEasy.setDefaultExtension('filename', '.js)

// f2 is filename.js (extension already exists)
let f2 = fileEasy.setDefaultExtension('filename.js', '.json')

// f3 is filename. (extension starts with . in original)
let f3 = fileEasy.setDefaultExtension('filename.', '.js')

.saveDocument()

const fileEasy = require('file-easy')

let filename = './docs/sample.txt'
let content = 'String to go in'

/**
 * Creates the `sample.txt` file in `./docs` folder
 * If path does not exist, it will create it (e.g. `./docs`)
 * The file is saved as a utf-8 format (standard format)
 */
fileEasy.saveDocument(filename, content)

Functions

setDefaultExtension(filename, extension) ⇒ string

Append specified extension if needed.

Kind: global function
Returns: string - filename with either existing or specified extension

ParamTypeDescription
filenamestringthe filename to check for an existing extension.
extensionstringthe extension to append if filename has no extension. It should start with a dot (e.g. .txt)

saveDocument(filename, content)

Save content in a file using utf8 format.

Kind: global function

ParamTypeDescription
filenamestringThe filename to create. It can also include a path ending with the filename. Path will be created if not exists.
contentstringThe content to place in the file.

slug(s) ⇒ string

Convert a string into an identifier.

Kind: global function
Returns: string - The identifier string

ParamTypeDescription
sstringThe string to convert by replacing special characters with dash (-)