Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

change-case

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

change-case - npm Package Compare versions

Comparing version 1.0.6 to 2.0.0

254

change-case.js

@@ -1,254 +0,116 @@

/**
* Wraps a function with a security check that ensures the argument being
* passed in is a string.
*
* @param {Function} fn
* @return {Function}
*/
var acceptString = function (fn) {
return function (string) {
if (typeof string !== 'string') {
throw new TypeError('Changing case will only works on strings');
}
var dot = require('dot-case');
var swap = require('swap-case');
var path = require('path-case');
var upper = require('upper-case');
var lower = require('lower-case');
var camel = require('camel-case');
var snake = require('snake-case');
var title = require('title-case');
var param = require('param-case');
var pascal = require('pascal-case');
var constant = require('constant-case');
var sentence = require('sentence-case');
return fn.apply(null, arguments);
};
};
/**
* Sanitizes a passed in string with certain options. It accepts a replacement
* function that should accept a string word parameter. It also accepts an
* optional separator override to replace the separator character.
* Check if a string is upper case.
*
* @param {String} string
* @param {Function} replacement
* @param {Function|String} [separator]
* @return {Array}
* @param {String} str
* @return {Boolean}
*/
var sanitizeString = function (string, replacement, separator) {
var index = -1;
return string
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.replace(/([^a-zA-Z0-9]*)([a-zA-Z0-9]*)/g, function (_, $0, $1) {
var prefix = $0;
index += 1;
if (prefix) {
if (typeof separator === 'string') {
prefix = separator;
} else if (typeof separator === 'function') {
prefix = separator($0, index, string);
}
}
return prefix + replacement($1, index, string);
});
exports.isUpperCase = function (str) {
return str === upper(str);
};
/**
* Exports object.
* Check if a string is lower case.
*
* @type {Object}
*/
var e = exports;
/**
* Expose a mutable array of insignificant words that will not be title cased.
*
* @type {Array}
*/
e.insignificantWords = ['and'];
/**
* Returns a boolean describing whether the string is all UPPERCASE or not.
*
* @param {String} string
* @param {String} str
* @return {Boolean}
*/
e.isUpperCase = acceptString(function (string) {
return string === e.upperCase(string);
});
exports.isLowerCase = function (str) {
return str === lower(str);
};
/**
* Returns a boolean describing whether the string is all lowercase or not.
* Upper case a string.
*
* @param {String} string
* @return {Boolean}
* @type {Function}
*/
e.isLowerCase = acceptString(function (string) {
return string === e.lowerCase(string);
});
exports.upperCase = exports.upper = upper;
/**
* Lowercase a passed in string.
* Lower case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.lowerCase = acceptString(function (string) {
return string.toLowerCase();
});
exports.lowerCase = exports.lower = lower;
/**
* Uppercase a passed in string.
* Title case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.upperCase = acceptString(function (string) {
return string.toUpperCase();
});
exports.titleCase = exports.title = title;
/**
* Uppercase the first character of a string.
* Sentence case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.upperCaseFirst = acceptString(function (string) {
return e.upperCase(string.charAt(0)) + string.substr(1);
});
exports.sentenceCase = exports.sentence = sentence;
/**
* Lowercase the first character of a string.
* Camel case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.lowerCaseFirst = acceptString(function (string) {
return e.lowerCase(string.charAt(0)) + string.substr(1);
});
exports.camelCase = exports.camel = camel;
/**
* Title case a passed in string. Pass an optional boolean flag to title case
* all words. Default behaviour is to skip insignicant words.
* Pascal case a string.
*
* @param {String} string
* @param {Boolean} significant
* @return {String}
* @type {Function}
*/
e.titleCase = e.title = acceptString(function (string, significant) {
return string
// Remove prefixed whitespace.
.replace(/^\s/, '')
// Remove suffixed whitespace.
.replace(/\s$/, '')
// Turn all whitespace into a single space character.
.replace(/\s+/g, ' ')
// Replace word strings. Matches "W.H.O", "tests'", "test's", "test", etc.
.replace(
/(?:(?:[a-zA-Z]\.)+[a-zA-Z]|[a-zA-Z\'\-]+[a-zA-Z]*)/g,
function (word, index) {
// Uppercase "W.H.O"
if (/(?:[a-zA-Z]\.)+[a-zA-Z]/g.test(word)) {
return e.upperCase(word);
}
exports.pascalCase = exports.pascal = pascal;
if (!significant && index > 0) {
if (word.length < 3 || ~e.insignificantWords.indexOf(word)) {
return e.lowerCase(word);
}
}
return e.upperCaseFirst(e.lowerCase(word));
})
// Fix broken sentence formatting.
.replace(/(\.\,\!\?\;)([a-zA-Z0-9]+) /g, '$1 $2');
});
/**
* Sentence case a passed in string. E.g. "TestString" => "test string".
* Snake case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.sentenceCase = e.sentence = acceptString(function (string) {
return sanitizeString(string, e.lowerCase, ' ');
});
exports.snakeCase = exports.snake = snake;
/**
* Pascal case a passed in string. E.g. "test string" => "TestString".
* Param case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.pascalCase = e.pascal = acceptString(function (string) {
return sanitizeString(string, function (word) {
return e.upperCaseFirst(e.lowerCase(word));
}, '');
});
exports.paramCase = exports.param = param;
/**
* Camel case a passed in string. E.g. "test string" => "testString".
* Dot case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.camelCase = e.camel = acceptString(function (string) {
return e.lowerCaseFirst(e.pascalCase(string));
});
exports.dotCase = exports.dot = dot;
/**
* Snake case a passed in string. E.g. "test string" => "test_string".
* Path case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.snakeCase = e.snake = acceptString(function (string) {
return sanitizeString(string, e.lowerCase, '_');
});
exports.pathCase = exports.path = path;
/**
* Param case a passed in string. E.g. "test string" => "test-string".
* Constant case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.paramCase = e.param = acceptString(function (string) {
return sanitizeString(string, e.lowerCase, '-');
});
exports.constantCase = exports.constant = constant;
/**
* Dot case a passed in string. E.g. "test string" => "test.string".
* Swap case a string.
*
* @param {String} string
* @return {String}
* @type {Function}
*/
e.dotCase = e.dot = acceptString(function (string) {
return sanitizeString(string, e.lowerCase, '.');
});
/**
* Path case a passed in string. E.g. "test string" => "test/string".
*
* @param {String} string
* @return {String}
*/
e.pathCase = e.path = acceptString(function (string) {
return sanitizeString(string, e.lowerCase, '/');
});
/**
* Constant case a passed in string. E.g. "test string" => "TEST_STRING".
*
* @param {String} string
* @return {String}
*/
e.constantCase = e.constant = acceptString(function (string) {
return sanitizeString(string, e.upperCase, '_');
});
/**
* Reverse the case of a string. E.g. "Test String" => "tEST sTRING".
*
* @param {String} string
* @return {String}
*/
e.switchCase = e.switch = acceptString(function (string) {
return string.replace(/[a-zA-Z]/g, function (c) {
var u = e.upperCase(c);
return c === u ? e.lowerCase(c) : u;
});
});
exports.swapCase = exports.swap = swap;
{
"name": "change-case",
"version": "1.0.6",
"description": "Convert strings between camelCase, PascalCase, Title Case, snake_case, etc",
"version": "2.0.0",
"description": "Convert a string between camelCase, PascalCase, Title Case, snake_case, etc.",
"main": "change-case.js",

@@ -9,14 +9,14 @@ "scripts": {

},
"repository": "https://github.com/blakeembrey/node-change-case",
"repository": "https://github.com/blakeembrey/change-case",
"keywords": [
"camelcase",
"pascalcase",
"titlecase",
"camel",
"pascal",
"title",
"case",
"lowercase",
"uppercase",
"paramcase",
"dotcase",
"pathcase",
"constantcase"
"lower",
"upper",
"param",
"dot",
"path",
"constant"
],

@@ -26,5 +26,19 @@ "author": "Blake Embrey",

"devDependencies": {
"mocha": "~1.17.1",
"istanbul": "~0.2.4"
"istanbul": "^0.2.6",
"mocha": "^1.18.2"
},
"dependencies": {
"camel-case": "0.0.2",
"constant-case": "0.0.2",
"dot-case": "0.0.2",
"lower-case": "0.0.1",
"param-case": "0.0.2",
"pascal-case": "0.0.2",
"path-case": "0.0.2",
"sentence-case": "0.0.2",
"snake-case": "0.0.2",
"swap-case": "0.0.1",
"title-case": "0.0.2",
"upper-case": "0.0.1"
}
}
/* global describe, it */
var assert = require('assert'),
changeCase = require('./');
var assert = require('assert');
var changeCase = require('./');

@@ -14,10 +14,2 @@ describe('change case', function () {

it('should lower case the first character', function () {
assert.equal(changeCase.lowerCaseFirst('TEST'), 'tEST');
});
it('should upper case the first character', function () {
assert.equal(changeCase.upperCaseFirst('test'), 'Test');
});
it('should determine whether a string is all upper case or not', function () {

@@ -37,9 +29,6 @@ assert.equal(changeCase.isUpperCase('ALLUPPERCASE'), true);

assert.equal(changeCase.titleCase('a test sentence'), 'A Test Sentence');
assert.equal(changeCase.titleCase('s.p.e.c'), 'S.P.E.C');
assert.equal(changeCase.titleCase('another s.p.e.c'), 'Another S.P.E.C');
assert.equal(changeCase.titleCase('i found a bug'), 'I Found a Bug');
assert.equal(changeCase.titleCase('i found a bug', true), 'I Found A Bug');
assert.equal(changeCase.titleCase('some things\''), 'Some Things\'');
assert.equal(changeCase.titleCase('"quotes"'), '"Quotes"');
assert.equal(changeCase.titleCase('hyphen-ness'), 'Hyphen-ness');
assert.equal(changeCase.titleCase('i found a bug'), 'I Found A Bug');
assert.equal(changeCase.titleCase('some things\''), 'Some Things');
assert.equal(changeCase.titleCase('"quotes"'), 'Quotes');
assert.equal(changeCase.titleCase('hyphen-ness'), 'Hyphen Ness');
});

@@ -116,6 +105,6 @@

it('should switch the cases', function () {
assert.equal(changeCase.switchCase('RaNdOMcasE'), 'rAnDomCASe');
assert.equal(changeCase.switchCase('mIX It.Down?'), 'Mix iT.dOWN?');
it('should swap the cases', function () {
assert.equal(changeCase.swapCase('RaNdOMcasE'), 'rAnDomCASe');
assert.equal(changeCase.swapCase('mIX It.Down?'), 'Mix iT.dOWN?');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc