Skip to content

kosperera/vanilla-js-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vanilla JavaScript { snippets }

JavaScript

I'm a huge nerd 🤓 of vanilla javascript (JS). I've built a few websites on it, as well as a few libraries and still has a long way to go to. This repository serves as a library of commonly used boilerplate code for all things vanilla javascript.

Read this article to learn more about this repository works.

On this repo

  • $qsa.js is to alias the document.querySelectorAll to traverse thru DOM elements
  • $.js is a micro-library for manipulating DOM as angular's $q
  • format-string.js is to replace strings as C# String.Format()
  • snake-case.js is to style strings to 'snake_case' as Lodash _.snakeCase()
  • mixin.js is to allow prototype option for helper functions as Lodash _.mixin()
  • script-data-src.js is magic code to replace <script> tags with attribute data-src with new HTMLScripElement

$qsa.js

$ aliases the document.querySelectorAll() to traverse thru DOM elements for manipulation.

$('a').each(function(el) { 
  el.addEventListener('input', console.log);
});

$.js

Micro-library for manipulating DOM as angular's jqLite but with most frequent use ones to manage classes, attributes, and to traverse thru DOM elements.

$('.highlight-source-js')
   .each(function(el) { 
    console.log(el.innterHTML);
  })
  .hide();

format-string.js

Replaces strings based on the format specified as C# String.Format().

_.format('{0} there! It's actually {1}', 'Hello', '/KP');
// expected output: 'Hello there! It's actually /KP'

snake-case.js

Style strings to 'snake_case' helper as Lodash _.snakeCase().

_.snakeCase('Library of vanilla JS snippets');
// expected output: 'library_of_vanilla_js_snippets'

mixin.js

Allow prototype option for helper functions as Lodash's _.mixin().

String.prototype.toSnakeCase = _.mixin(_.snakeCase);
// or
_.mixin(_.snakeCase, String, 'toSnakeCase');

// then use as
sometext.toSnakeCase();

script-data-src.js

Replace <script> tags with attribute data-src with new HTMLScripElement and attributes.

<script async
      data-src="../scripts/tokenizer.js" 
      data-client-id="{{client_id}}">
</script>
<script async
      data-src="../scripts/gtm.js" 
      data-client-id="{{ga-key}}">
</script>
<script async src="script-data-src.js"></script>

Articles

Contributing

If you were wondering, I'd be happy to have more code snippets here. Have a suggestion or a bug fix? Just open a pull request or an issue. Include the code snippet with a clear file name and the simplest HTML possible.

License

Licensed under MIT

About

Library of commonly used boilerplate code for all things vanilla JS

Topics

Resources

License

Stars

Watchers

Forks