UNPKG

@amindunited/to-snake-case

Version:

Accepts a, Title Case, camel Case or dash separated string and returns a copy separated by underscores.

25 lines (20 loc) 595 B
/** * @license * Copyright Robin Buckley. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file */ 'use strict'; const toSnakeCase = function (originalName) { let stringContentArray = []; if ( originalName.indexOf('-') > 0) { stringContentArray = originalName.split(/-/); } else { stringContentArray = originalName.split(/(?=[A-Z])/) } return stringContentArray.reduce((previous, current) => { return previous.toLowerCase() + '_' + current.toLowerCase(); }); } module.exports = toSnakeCase;