Skip to content

ProJSLib/jsbits

Repository files navigation

JSBits

Public library of professional high-quality functions for JavaScript and TypeScript.

License AppVeyor test Travis test coverage code quality maintainability

The functions of JSBits are written in TypeScript 3 and meet stringent quality requirements.
They were designed to be efficient and robust, work in strict mode and are tested with 100% coverage.

Most of the functions are pure, isomorphic, and do not have external dependencies, except for a few that, due to their purpose, only run on node.js and use its native library.

Installation

This is a... mini-monorepo?... whatever.

This is a centralized repository that standardizes the development and testing of JSBits. To use, each function must be installed from its own npm package or directly in the browser, so you get exactly what you need, without garbage or longer download times.

The section Distribution formats details the support for several bundlers and the README of each function contains its requirements and download options.

NOTE: This work is in its infancy, I will be adding more functions as my time allows.

Functions

Adds or subtracts N months to any JavaScript Date, local or UTC.

v1.1.2 – Group: date – Author: aMarCruz

Performs a deep cloning of an object own properties and symbols, with loosy or exact behavior.

v1.1.1 – Group: object – Author: aMarCruz

Calculates the local date of the Easter day for years up to 9999.

v1.0.1 – Group: date – Author: aMarCruz

Escapes special characters in a string, for matching substrings with a RegExp instance.

v1.0.3 – Group: dev – Author: aMarCruz

NodeJS only.

Get the version of the package.json file found in the given directory or in one of its parents.

v1.0.3 – Group: dev – Author: aMarCruz

Determines whether an object has own properties or symbols, including (optionally) the non-enumerable ones.

v1.1.1 – Group: object – Author: aMarCruz

Distribution Formats

This is a list of the fields of package.json used by JSBits to facilitate the detection of the correct format for your toolchain and the default format used by some bundlers, loaders, and other tools for which I have information:

  main browser module es2015/esnext $.jsbits/jsbits
Filename index.js umd/index.js esm/index.js index.mjs index.b.min.js
ES version ES5 ES5 ES5 ES6 ES5
Module format CJS UMD ESM ESM IIFE
node.js* - - -
Brunch - - - -
jspm - - - -
browserify - - - -
Rollup - - - -
webpack - - -
TypeScript - - - -
jQuery - - -

In one way or another, most modern bundlers let you configure the field used to resolve imports –this includes webpack, jspm, browserify, and Rollup–, but even without this support, you can direct your tool to the desired format using its folder and/or extension.

Surprisingly, one such tool is TypeScript, which always uses the "main" field, at least until v3.1.x, so if you are using ESM imports you need enable esModuleInterop in your tsconfig.json or import the function by its filename, like this:

import addMonths from '@jsbits/add-months/esm'         // ES5 variant
import addMonths from '@jsbits/add-months/index.mjs'   // ES6 variant

The import = require() syntax does not have this restriction. In any case, typings must to work.

BTW, the last import in the example is also used by node.js with ES Modules support.

* The file imported by node depends on the use of the --experimental-modules flag.

About the jsbits object

This UMD version, pointed by the "browser" field, works with AMD and CJS module systems but if no one is detected, it adds the function in the jQuery.jsbits namespace, if this already exists, or in the global jsbits object (i.e. window.jsbits).

If you dont need AMD/CJS support, you can load the functions from the index.b.min.js file.

The following example loads $.jsbits.addMonths in the browser:

<script>$.jsbits = {};</script>
<script src="https://unpkg.com/@jsbits/add-months/index.b.min.js"></script>

TODO

In addition to increasing the number of functions...

  • Test in browser and badge of the results
  • Packages of group of functions
  • Better docs and contributors guides
  • A more robust structure of the repo and its commands

Support my Work

I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...

Of course, feedback, PRs, and stars are also welcome 🙃

Thanks for your support!

Thanks

Special thanks to BrowserStack for their excellent service.

License

The MIT License.

© 2018-2019 Alberto Martínez