Code Monkey home page Code Monkey logo

mockingcase's Introduction

mockingcase Build Status Hits-of-Code Hits

Convert a string to mOcKiNgCaSe.

Read more on wikipedia Studly caps.

mocking spongebob

Inspired by the meme Mocking Spongebob and http://dannypage.github.io/spongebob.html

Install

npm install @strdr4605/mockingcase --save

Usage

const mockingcase = require('@strdr4605/mockingcase');
// es6 - import mockingcase from '@strdr4605/mockingcase';
//  ts - import * as mockingcase from '@strdr4605/mockingcase';
//  ts - import mockingcase = require('@strdr4605/mockingcase');

mockingcase('foo-bar');
//=> 'fOo-bAr'

mockingcase('aa', {random: false});
//=> 'aA'

mockingcase('aa', {random: true});
//=> 'aa'
//=> 'aA'
//=> 'Aa'
//=> 'AA'

mockingcase('42foo!bar');
//=> '42fOo!bAr'

mockingcase('aa123', {onlyLetters: true});
//=> 'aA'

mockingcase('a13%$a', {onlyLetters: true});
//=> 'aA'

mockingcase('foo bar', {firstUpper: true});
//=> 'FoO BaR'

mockingcase('foo', {firstUpper: true, random: true});
//=> 'Foo'
//=> 'FOo'
//=> 'FoO'
//=> 'FOO'

mockingcase('abcdef', {upper: /[bdf]/});
//=> 'aBcDeF'

mockingcase('ABCDEF', {lower: 'bcd'});
//=> 'abcdeF'

API

Functions

mockingcase(input, [options])string

Converts the input string(s) to mOcKiNgCaSe.

Typedefs

Options : Object

Options for mockingcase

mockingcase(input, [options]) ⇒ string ⬆️

Converts the input string(s) to mOcKiNgCaSe.

Kind: global function Returns: string - string in mOcKiNgCaSe

Param Type Default Description
input string | Array.<string> String(s) to be converted.
[options] Options {random: false, onlyLetters: false, firstUpper: false, upper: '', lower: ''} Conversion options.
mockingcase('foo-bar');
//=> 'fOo-bAr'

mockingcase('aa', {random: false});
//=> 'aA'

mockingcase('aa', {random: true});
//=> 'aa'
//=> 'aA'
//=> 'Aa'
//=> 'AA'

mockingcase('42foo!bar');
//=> '42fOo!bAr'

mockingcase('aa123', {onlyLetters: true});
//=> 'aA'

mockingcase('a13%$a', {onlyLetters: true});
//=> 'aA'

mockingcase('foo bar', {firstUpper: true});
//=> 'FoO BaR'

mockingcase('foo bar', {firstUpper: true, lower: /[fb]/});
//=> 'foO baR'

mockingcase('foo bar', {firstUpper: true, upper: /[oa]/});
//=> 'FOO BAR'

mockingcase('foo', {firstUpper: true, random: true});
//=> 'Foo'
//=> 'FOo'
//=> 'FoO'
//=> 'FOO'

mockingcase(['foo','bar']);
//=> 'fOoBaR'

mockingcase(undefined);
//=> Error "An input is required"

mockingcase.overrideString() ⇒ mockingcase ⬆️

Creates String.prototype.toMockingCase().

Kind: static method of mockingcase
Returns: mockingcase

mockingcase.overrideString();

'foo_bar'.toMockingCase();
//=> 'fOo_bAr'

'foo_bar'.toMockingCase({firstUpper: true});
//=> 'FoO_BaR'

String.prototype.toMockingCase([options]) ⇒ string ⬆️

Converts this string to mOcKiNgCaSe.

NOTE: this function is created by invoking mockingcase.overrideString().

Kind: prototype
Returns: string - local string in mOcKiNgCaSe

Param Type Default Description
input string | Array.<string> String(S) to be converted.
[options] Options {random: false, onlyLetters: false, firstUpper: false, upper: '', lower: ''} Conversion options.
'foo_bar'.toMockingCase();
//=> 'fOo_bAr'

'foo_bar'.toMockingCase({firstUpper: true});
//=> 'FoO_BaR'

mockingcase.config(defaultOptions) ⇒ mockingcase ⬆️

Outputs a mockingcase with default options.

Kind: static method of mockingcase
Returns: mockingcase with default options

Param Type Default Description
defaultOptions Options {random: false, onlyLetters: false, firstUpper: false, upper: '', lower: ''} Conversion options.
const mockingcase = require('@strdr4605/mockingcase').config({onlyLetters: true, firstUpper: true});
// const mOcKiNgCaSe = mOcKiNgCaSe.config({onlyLetters: true, firstUpper: true});

mockingcase('foo bar42');
//=> 'FoO BaR'

mockingcase('foo bar42', {onlyLetters: false, firstUpper: false});
//=> 'fOo bAr42'

mockingcase.log(input, [options]) ⬆️

Outputs a message to the console in mOcKiNgCaSe.

Kind: static method of mockingcase

Param Type Default Description
input string | Array.<string> String(S) to be converted.
[options] Options {random: false, onlyLetters: false, firstUpper: false, upper: '', lower: ''} Conversion options.
mockingcase.log('foo bar');
// console.log('fOo bAr');


mockingcase.overrideConsole([options]) ⇒ mockingcase ⬆️

Overrides console.log input to print the input mOcKiNgCaSe.

Kind: static method of mockingcase
Returns: function - mockingcase function
See: mockingcase

Param Type Default Description
[options] Options {random: false, onlyLetters: false, firstUpper: false, upper: '', lower: ''} Conversion options.
const mockingcase = require('@strdr4605/mockingcase').overrideConsole();
console.log('foobar')
// => 'fOoBaR'
mockingcase('foobar');
// => 'fOoBaR'

Options : Object ⬆️

Options for mockingcase

Kind: global typedef
Properties

Name Type Default Description
[random] boolean false If case conversion should be randomized.
[onlyLetters] boolean false If non letters characters should be removed.
[firstUpper] boolean false If the first letter should be capitalized instead of the second when converting to mOcKiNgCaSe (e.g. MoCkInGcAsE). When combined with options.random, the first letter of the random string will be capitalized.
[upper] string | RegExp '' Characters or substring set to change to uppercase, upper has higher priority that lower.
[lower] string | RegExp '' Characters or substring set to change to lowercase.

Browser Usage ⬆️

mOcKiNgCaSe can be used in a node environment, as well as in the browser. You can serve it yourself, or pull it from a CDN. For example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>mockingcase</title>
</head>
<body>
</body>
<script src="https://unpkg.com/@strdr4605/[email protected]/src/mockingcase.js"></script>
<script>
  const output = mockingcase('foo-bar');
  console.log(output);
  const output2 = mockingcase('foo-bar');
  console.log(output2);
</script>
</html>

Self Hosting

To host mockingcase yourself simply put src/mockingcase.js wherever your static content (like CSS stylesheets) are kept. You can also download a minified file from one of the CDNs below.

CDN Usage

Simply pull in one of the following JS files below.

Name Link
unpkg.com https://unpkg.com/@strdr4605/mockingcase
JSDelivr.com https://cdn.jsdelivr.net/npm/@strdr4605/mockingcase

See also Mockingcase bindings for ReasonML

mockingcase's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mockingcase's Issues

Add firstUpper option

Currently, the package converts even positions of characters to lowercase and odd positions to uppercase.
firstUpper options should reverse the converting pattern.
Example:

const mOcKiNgCaSe = require('mockingcase');
mOcKiNgCaSe('example'); // eXaMpLe
mOcKiNgCaSe('example', {firstUpper: true}); //  ExAmPlE

if both random and firstUpper options are set to true first character should always be uppercase

Fix mOcKiNgCaSe.config overridedDefaultOptions?

At the moment overriding default options from mOcKiNgCaSe.config

const options = overridedDefaultOptions || defaultOptions;

maybe is better to merge defaultOptions with overridedDefaultOptions and NOT just replace it?!

Add @typedef for options

Description (Motivation)

As "options" param is used in several functions and writing each time

/**
 * @param {object} [options={random: false,  onlyLetters: false, firstUpper: false, upper: null, lower: null}] Conversion options.
 * @param {boolean} options.random=false - If case conversion should be randomized.
 * @param {boolean} options.onlyLetters=false - If non letters characters should be removed.
 * @param {boolean} options.firstUpper=false - If the first letter should be capitalized instead of the second when converting to mOcKiNgCaSe (e.g. MoCkInGcAsE). When combined with options.random, the first letter of the random string will be capitalized.
 * @param {(string | RegExp)} options.upper=null - Characters or substring set to change to uppercase
 * @param {(string | RegExp)} options.lower=null - Characters or substring set to change to lowercase
*/

is not good approach as somewhere it can be missed (forgotten to update), which can lead to confusion in types.
Creating a typedef and using it where it is needed is the way to go.

Possible solution

Not sure yet but maybe examples from https://jsdoc.app/tags-typedef.html can help

TODO:

  • Add @typedef for options (problable at the beginning of the file)
  • Replace type of params with new @typedef for options
  • Check generated markdown with jsdoc2md and maybe update README

Move main files to src folder

As the repo has a lot of files, it became a little messy. In discussion #52 (comment)
I think is better to add an src folder with this structure

  • src
    • mockingcase.d.ts
    • mockingcase.js
    • mockingcase.test.js

TO DO:

  • Create src folder
  • Rename and move main files to src folder
  • Update package.json
  • Check that tests, pre-commit, prettier and eslint works properly
  • Check that Declaration Files for Typescript works properly

Refactor code according to jshint

https://jshint.com/ says that this is the best practice

(function () {
  'use strict';
  
  /**
   * This function receives a string input and converts it to mOcKiNgCaSe.
   * @param {string} input - string to be converted
   * @param {object} [options={random: false}] - options for converting
   * @param {boolean} options.random=false - using random for converting
   * @returns {string} string in mOcKiNgCaSe
   */
  function mOcKiNgCaSe(input, options) {
    options = Object.assign({
      random: false
    }, options);
    
    return input.replace(/./g, function (s, i) {
      if(options.random) return Math.round(Math.random()) ? s.toUpperCase() : s.toLowerCase();
      else return i % 2 ? s.toUpperCase() : s.toLowerCase();
    });
  }
  
  module.exports = mOcKiNgCaSe;
})();  

should think if to add jshint.json or eslint

Fix Browser Usage link in table of contents

When clicking on the Browser Usage from API table of contents it does not jump to the sections

A possible solution is to add a a tag like in this example

- [mOcKiNgCaSe(input, [options]) ⇒ <code>string</code>](#mOcKiNgCaSe)
<a name="mOcKiNgCaSe"></a>

Add config for options on importing the package

Would be good to have a config function that can be used to create default options.
For example instead of:

const mOcKiNgCaSe = require('mockingcase');
mOcKiNgCaSe('foo bar42', {onlyLetters: true, firstUpper: true});
//=> 'FoO BaR'

mOcKiNgCaSe('foo bar', {firstUpper: true});
//=> 'FoO BaR'

to have this possibility:

const mOcKiNgCaSe = require('mockingcase').config({onlyLetters: true, firstUpper: true});
// const mOcKiNgCaSe = require('mockingcase');
// mOcKiNgCaSe.config({onlyLetters: true, firstUpper: true});

mOcKiNgCaSe('foo bar42');
//=> 'FoO BaR'

mOcKiNgCaSe('foo bar');
//=> 'FoO BaR'

and when including options for a specific case, to override selected config options:

const mOcKiNgCaSe = require('mockingcase').config({onlyLetters: true, firstUpper: true});
// const mOcKiNgCaSe = require('mockingcase');
// mOcKiNgCaSe.config({onlyLetters: true, firstUpper: true});

mOcKiNgCaSe('foo bar42');
//=> 'FoO BaR'

mOcKiNgCaSe('foo bar42', {onlyLetters: false, firstUpper: false});
//=> 'fOo bAr42'

config function should return modified mOcKiNgCaSe

TO DO:

  • Add config function and its implementation.
  • Comment it using jsdoc
  • Add 2-3 unit tests for this behavior
  • Add declaration of config function for Typescript, Declaration Files
  • Add usage and describe config function in README

Remove unnecessary links from table of contents

Having subsections links (Self Hosting, CDN Usage) in the table of contents for Browser Usage is not so important.

Better to remove them as Browser Usage is short enough to read every subsection.

Add option that converts only specific letters

Option object should have these fields:
upper should be of type string or instanceof RegExp
lower should be of type string or instanceof RegExp
else

throw TypeError(`options.upper: Expected string or RegExp but got "${typeof upper}"`);

The RegExp will be created as:

if (options.upper) const upperRegExp = new RegExp(options.upper, 'gi');
if (options.lower) const lowerRegExp = new RegExp(options.lower, 'gi');

Possible solution:

Step 1:
Convert the input with default behavior (even prostion: lowerCase; odd position: upperCase)
or if any option is present that do it first:

  • remove non letters if onlyLetters
  • odd prostion: lowerCase; even position: upperCase if firstUpper
  • random case if random

Step 2:

return step1Input
.replace(lowerRegExp, toLowerCase).
.replace(upperRegExp, toUpperCase);

upper has a higher priority so convert letters that much that regex after conversion of lower

Expected:

mockingcase("Hello World", { upper: "worl" });
// "hElLo WORLd"
mockingcase("Hello World", { upper: /worl/ });
// "hElLo WORLd"

mockingcase("Hello World", { upper: "[worl]" });
// "hELLO WORLd"
mockingcase("Hello World", { upper: /[worl]/ });
// "hELLO WORLd"

mockingcase("Hello World", { upper: /[worl]/, firstUpper: true });
// "HELLO WORLd"

TO DO:

  • Add upper and lower options and implement them.
  • Comment options using jsdoc
  • Add unit test for upper and lower options
  • Add declaration of upper and lower options for Typescript, Declaration Files
  • Add usage and describe upper and lower options in README

Status:

@cukejianya is working on this!

Change default value for upper, lower options.

Description (Motivation)

Currently, default values for upper and lower options are null, but in jsdocs type is set to string or RegExp. Probably this is not correct.

Possible solution

Change the default value to be the empty string ''.

TODO:

  • Implement changes
  • Comment using jsdoc
  • Update docs in README

Add default value for input or throw an error

If the input is not provided the default value for it should be something like:
iNpUt iS UnDeFiNeD

const mockingcase = require('mockingcase');
mockingcase();
// => 'iNpUt iS UnDeFiNeD'

OR maybe throw an error like for array of string case

TO DO:

  • Add defaut input value for input to main function
  • Add default input value in jsdoc
  • Add unit test for checking default input value
  • Add usage and describe about default input value in README

add options

  1. Randomness
  2. Another type of randomness
  3. specific offset
  4. ...

add a log function to display converted string

Would be nice to have a function that we can call like

const mOcKiNgCaSe = require('mockingcase');

const text = mOcKiNgCaSe('foo-bar');
console.log(text); // fOo-bAr

mOcKiNgCaSe.log('foo-bar'); // fOo-bAr

example and possible solution:

function a() {
	a.log = () => console.log('A');
	return 'a';
}

let f = a;
console.log(f()); // a
f.log(); // A

Add functionality to override console.log

Would be nice to have a functionality that overrides all console.logs.
Example:

mockingcase.overrideConsole();
console.log('Hello'); // hElLo

TO DO:

  1. Let the overrideConsole return the mockingcase object as overrideString so it can be used at importing like:
const mOcKiNgCaSe = require('mockingcase').overrideConsole();
console.log('foobar')
// => 'fOoBaR'
mOcKiNgCaSe('foobar');
// => 'fOoBaR'
  1. Add jsdoc and unit tests
  2. Add overrideConsole in the index.d.ts file, so it can be used with typescript
  3. Add usage in README after
// Optionally create String.prototype.toMockingCase
mOcKiNgCaSe.overrideString();

'foo_bar'.toMockingCase();
//=> 'fOo_bAr'

Change usage in README

The Usage section is too big and it could grow in future.
In the main Usage section only mOcKiNgCaSe(input, [options]) usage should be sampled.

Usages for others functionalities of the package should be sampled bellow the API description of functions parameters.

Add functionality that changes all text on an HTML page

Would be nice to use this package as a 1st of April Joke. By using this package in any frontend library/framework build with npm.

A possible solution is to check if it is a frontend app and has HTML, them finding every text on the page and convert it to mockingcase!

Welcome for discussions on this topic! 😊

TO DO:

  • Pick a good name for the function
  • Implement the functionality that will change all text (after each DOM update)
  • Comment the function using jsdoc
  • Add unit test for the function
  • Add declaration of the function for Typescript, Declaration Files
  • Add usage and describe the function in README

Update:

Found a solution to select all nodes with text from a page.
https://stackoverflow.com/questions/2579666/getelementsbytagname-equivalent-for-textnodes#2579869

Update2:

related topic: https://stackoverflow.com/questions/3219758/detect-changes-in-the-dom

String.prototype.toMockingCase();

Another cool idea would be to call mockingcase from String object

"foobar".toMockingCase();
// => 'fOoBaR'

maybe to override String object by default when importing package

require('mockingcase');

or by calling an obvious function

require('mockingcase').overrideString();

If user somehow whats to use both implementations, overrideString() should return also main function of the package

const mOcKiNgCaSe = require('mockingcase').overrideString();
'foobar'.toMockingCase();
// => 'fOoBaR'
mOcKiNgCaSe('foobar');
// => 'fOoBaR'

TO DO:

  • Add overrideString() that override String object by adding toMockingCase()
  • Comment overrideString() and toMockingCase() using jsdoc
  • Add unit test for toMockingCase()
  • Add declaration of toMockingCase() for Typescript, Declaration Files
  • Add usage and describe overrideString() and toMockingCase() in README

Rename mOcKiNgCaSe in mockingcase

As discussed in #52 and as runkit sample

var mockingcase = require("@strdr4605/mockingcase")

the main function should be renamed in mockingcase

TO DO:

  • Change all mOcKiNgCaSe in mockingcase for main file
  • Change all mOcKiNgCaSe in mockingcase for test file
  • Change all mOcKiNgCaSe in mockingcase in README

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.