Skip to content

Instantly share code, notes, and snippets.

View LeanSeverino1022's full-sized avatar

Leandro Severino LeanSeverino1022

View GitHub Profile
@LeanSeverino1022
LeanSeverino1022 / index.js
Last active October 8, 2022 17:36
pretty pring object #javascript
const output = JSON.stringify(myObject, '\t', 2);
//sample
var content = JSON.stringify({
test: 2,
game: 5
}, '\t', 2);
@LeanSeverino1022
LeanSeverino1022 / script.js
Created September 25, 2022 07:23
percent to decimal and vice-versa #numbers
function pctToDecimal(percent) {
return parseFloat(percent) / 100;
}
/*
pctToDecimal("2xx%")
0.02
pctToDecimal("xx%")
NaN
pctToDecimal("50")
@LeanSeverino1022
LeanSeverino1022 / snippet.md
Last active April 3, 2021 08:09
check if a string contains digits js #strings #vanillajs

make use of RegExp.prototype.test()

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

function hasNumber(myString) {
  return /\d/.test(myString);
}
@LeanSeverino1022
LeanSeverino1022 / console.md
Created April 1, 2021 12:36
console.log formatting #vanillajs #debugging

Examples

window.myConsole = {};
myConsole.log = msg => {
    console.log('%c%s', 'color: #fff; background: red; font-size: 14px;', msg);
}
@LeanSeverino1022
LeanSeverino1022 / test.md
Created April 1, 2021 12:05
Check if a string contains any item from a substring array #strings #vanillajs

Using some and includes

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}

Example

@LeanSeverino1022
LeanSeverino1022 / sample.js
Created March 30, 2021 08:27
multiples ifs to each (DRY) #designPatterns
// BAD
if ( eventfade.data( "currently" ) !== "showing" ) {
eventfade.stop();
}
if ( eventhover.data( "currently" ) !== "showing" ) {
eventhover.stop();
}
if ( spans.data( "currently" ) !== "showing" ) {
@LeanSeverino1022
LeanSeverino1022 / readme.md
Last active March 25, 2021 09:07
Avoiding Conflicts with Other Libraries #jQuery #ajax

👍🏻 OK

Ways

Here's a recap of ways you can reference the jQuery function when the presence of another library creates a conflict over the use of the $ variable:

Create a New Alias

The jQuery.noConflict() method returns a reference to the jQuery function, so you can capture it in whatever variable you'd like:

@LeanSeverino1022
LeanSeverino1022 / notes.md
Last active March 20, 2021 03:39
Mutating vs. Non-Mutating Array methods #vanillajs

.reviewed

If an item is mutable, modifying the copy also modifies the original. If it’s immutable, modifying the copy does not affect the original. It’s confusing because immutable sounds like the item can’t be changed. What it actually means, though, is that the original is not changed when the copy is.

array.splice() mutates the original array, (changes the contents of an array) array.slice() does not mutate the original array (returns a shallow copy)

Adding items

Mutating: 'array.push() and array.unshift()

@LeanSeverino1022
LeanSeverino1022 / refs.md
Last active March 18, 2021 03:39
Basics of Refs #vue

ref="userText" - Vue detects such refs and stores them internally. It basically memorizes that you want access to elements and in your js code you can have access to these elements.

$refs - is an object full of key value pairs where the keys are ref identifiers you set up in your template

ex: this.message = this.$refs.userText.value; console.log(this.$refs.userText) // the whole element will be seen

@LeanSeverino1022
LeanSeverino1022 / method1.md
Last active January 25, 2021 07:59
Detect if using a mobile device #vanillajs

02/2021

Multiple resources suggest using the Navigator userAgent property to detect mobile devices. However you must take into account that maybe it's not 100% reliable and some devices could give you "false" (I haven't found any problem yet though).

You can use either match or test.

There are alos different ways of doing it but I just used what I think is efficient.

You can also make use of simple media query but there are cases you would prefer not to use that method.