strdr4605 / mockingcase Goto Github PK
View Code? Open in Web Editor NEWnode package that converts a string to mOcKiNgCaSe
Home Page: https://npm.im/@strdr4605/mockingcase
License: ISC License
node package that converts a string to mOcKiNgCaSe
Home Page: https://npm.im/@strdr4605/mockingcase
License: ISC License
This error is showing up when the package is used from CDN
Possible CDNs to work with:
site | CDN |
---|---|
https://unpkg.com | https://unpkg.com/[email protected]/index.js |
https://www.jsdelivr.com | https://cdn.jsdelivr.net/npm/[email protected]/index.min.js |
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
src
foldersrc
folderpackage.json
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.
Not sure yet but maybe examples from https://jsdoc.app/tags-typedef.html can help
@typedef
for options (problable at the beginning of the file)@typedef
for optionsCurrently, there are no unit tests for option {random: true}
. Should discuss possible test checks for this 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
mOcKiNgCaSe
be exported as default?mOcKiNgCaSe
function be renamed in mockingcase
?index
or mockingcase
name?mOcKiNgCaSe(['foo', 'bar']);
//=> 'fOoBaR'
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.
Change the default value to be the empty string ''
.
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?!
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>
At the moment the String.prototype.toMockingCase()
does not support packages options ({random: true, ...}
).
Also better to use arrow function for it.
https://github.com/strdr4605/mockingcase/blob/master/index.js#L76
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.
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();
overrideString()
should return also main function of the packageconst mOcKiNgCaSe = require('mockingcase').overrideString();
'foobar'.toMockingCase();
// => 'fOoBaR'
mOcKiNgCaSe('foobar');
// => 'fOoBaR'
String
object by adding toMockingCase()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! ๐
Found a solution to select all nodes with text from a page.
https://stackoverflow.com/questions/2579666/getelementsbytagname-equivalent-for-textnodes#2579869
related topic: https://stackoverflow.com/questions/3219758/detect-changes-in-the-dom
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
create index.d.ts
file with types for params and return value
https://github.com/strdr4605/mockingcase/blob/master/index.js
Add โฌ๏ธ to every section.
[:arrow_up:](#api)
@strdr4605 Hey would be nice if you upload the linter config you are using so that everybody can use the same.
Originally posted by @RicardoE105 in #42 (comment)
Would be nice to have a functionality that overrides all console.log
s.
Example:
mockingcase.overrideConsole();
console.log('Hello'); // hElLo
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'
overrideConsole
in the index.d.ts
file, so it can be used with typescript// Optionally create String.prototype.toMockingCase
mOcKiNgCaSe.overrideString();
'foo_bar'.toMockingCase();
//=> 'fOo_bAr'
Mockingcase has a bindings for ReasonML / bucklescript, bs-mockingcase.
Issue is described here strdr4605/bs-mockingcase#1
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');
Step 1:
Convert the input with default behavior (even prostion: lowerCase; odd position: upperCase)
or if any option is present that do it first:
onlyLetters
firstUpper
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
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"
@cukejianya is working on this!
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
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.
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
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
mOcKiNgCaSe('42foo!bar', {letters: true});
//=> 'fOoBaR'
mOcKiNgCaSe('foo bar 42', {letters: true});
//=> 'fOoBaR'
โ Maybe think about a better name for option
Array.from(input)
.map((char, index) => char[index % 2 ? 'toUpperCase' : 'toLowerCase']())
.join('');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.