Giter VIP home page Giter VIP logo

micell's Introduction

micell

Build Status codecov npm npm bundle size NPM Netlify Status

English | 简体中文

A collection of functions for web development.

  • Base64 encoding and decoding a string or binary data
  • Character range checking
  • Cookie manipulation
  • Date diff and format
  • DOM computing and manipulation
  • Easing functions
  • Deciding the type of Javascript value
  • File path operation
  • Querystring parse and stringify
  • Common regular expressions
  • String manipulation
  • UserAgent detection
  • Ajax, css, jsonp, random string, uuid and more.

Install

Npm

npm i -S micell

Yarn

yarn add micell

CDN

If you want use micell with <script> directly, you can use jsDelivr.

The latest version:

<script src="https://cdn.jsdelivr.net/npm/micell"></script>

The specific version:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/micell.js"></script>

The ES Modules version:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/micell.esm.browser.js"></script>

The micell also exists in unpkg.

Usage

import micell from 'micell'

// Generate a random string
micell.randomString();

// Get a cookie value
micell.cookie.get('name')

More functions see the Docs.

Reduce the bundle size

You can use the babel-plugin-lodash to bundle the methods as you needed.

.babelrc

{
  "plugins": [
    ["lodash", { "id": ["micell"] }]
  ]
}

Docs

See Docs.

Compatibility

  • Chrome
  • Firefox
  • Safari
  • Edge
  • IE >= 11
  • iOS >= 10
  • Android >= 5

Changelog

See Release notes.

Contributing

If you have a bug or feature request or document improvement about micell, you can open an issue or create a pull request to main branch.

Also, you can read the CONTRIBUTING guide.

License

MIT

Copyright (c) 2019-preset, Alex Chao

micell'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

Watchers

 avatar  avatar

micell's Issues

Add pageWidth and pageHeight methods

The pageWidth method returns the width of current page. The pageHeight method returns the height of current page.The current page is referred as the rendering document or any DOM element.

  • The width of current page is equal to the visual width plus the scroll width.
  • The height of current page is equal to the visual height plus the scroll height.

Is it necessary to add the yarn.lock file?

Github dependabot alerts show the below warning message:

Dependabot can't update vulnerable dependencies without a lockfile
Dependabot can't update vulnerable dependencies for projects without a package-lock.json or yarn.lock file as the currently installed version can't be determined.
To resolve the issue add a supported lockfile (package-lock.json or yarn.lock).
View logs or learn more about troubleshooting Dependabot errors.

Add roundFloat, ceilFloat and floorFloat methods

The roundFloat/ceilFloat/floorFloat methods is similar to Math.round/Math.ceil/Math.floor for integers, these are used for decimals.

Sometimes, we need to trim the fractional part to the specified length with floor round. For example:

let pi = 3.141592654

// Retain four decimal places
let displayPi = Math.floor(pi * 10000) / 10000
// => 3.1415

The date diff document does not reflect the actual result.

The example from document is below:

const date1 = new Date(2019, 1, 10, 0, 0, 0);
const date2 = '2019-01-01 00:00:00';
diff(date1, date2);
// => 3456000000
diff(date1, date2, 'D')
// => 40

But, the actual result is below, so the order of parameters is reversed. May we modify the document to reflect the actual.

const date1 = new Date(2019, 1, 10, 0, 0, 0);
const date2 = '2019-01-01 00:00:00';
micell.date.diff(date1, date2);
// => -3456000000
micell.date.diff(date1, date2, 'D')
// => -40

The tslib should be not bundled.

The below error is thrown in a Vite + vue3 project.

X [ERROR] Could not resolve "tslib"

    node_modules/micell/dist/micell.esm.js:1:25:
      1 │ import { __assign } from 'tslib';
        ╵                          ~~~~~~~

  You can mark the path "tslib" as external to exclude it from the bundle, which will remove this
  error.

Improve numberFormat algorithm

Using split/reverse/join methods is bad. Using the below code:

function numberFormat(num) {
  const abs = Math.abs(num)
  const intPart = Math.floor(abs)
  const intPartStr = intPart.toString()
  const decimalPart= abs - intPart
  const decimalPartStr = abs - intPart === 0 ? '' : num.toString().split('.')[1]
  const intLen = intPartStr.length;
  const start = intLen % 3
  let newIntPartStr = ''
  for (let i = 0; i < intLen; i++) {
    newIntPartStr += intPartStr[i]
    if ((i + 1 - start) % 3 === 0 && i !== intLen- 1) {
      newIntPartStr += ','
    }
  }
  return (num >= 0 ? '' : '-') + newIntPartStr + (decimalPartStr ? `.${decimalPartStr}` : '')
}

console.log(numberFormat(0))
console.log(numberFormat(0.1))
console.log(numberFormat(1.1))
console.log(numberFormat(1234))
console.log(numberFormat(1234567.1234))
console.log(numberFormat(12345678.1234))
console.log(numberFormat(123456789.1234))
console.log(numberFormat(-0))
console.log(numberFormat(-0.1))
console.log(numberFormat(-1.1))
console.log(numberFormat(-1234))
console.log(numberFormat(-1234567.1234))
console.log(numberFormat(-12345678.1234))
console.log(numberFormat(-123456789.1234))

Output:

0
0.1
1.1
1,234
1,234,567.1234
12,345,678.1234
123,456,789.1234
-0
-0.1
-1.1
-1,234
-1,234,567.1234
-12,345,678.1234
-123,456,789.1234

While, we should add benchmark to compare the performance.

Proposal: classnames

When writing base ui component, generating className property by multiple properties is common.

For example, a button component.

Without classnames

classs Button {
  public get className() {
    let cls = ''
    cls += ` btn-${this.type}`
    cls += ` btn-${this.size}`
    cls += ` btn-${this.shape}`
    cls += this.block ? ' btn-block' : ''
    return cls
  }
}

With classnames

classs Button {
  public get className() {
    return classnames({
      [`btn-${this.type}`]: 1,
      [`btn-${this.size}`]: 1,
      [`btn-${this.shape}`]: 1,
      'btn-block': this.block
  })
}

This function is same as classnames package.

Support accepting a Blob object for base64/encode

We may need convert an uploaded image to base64. An uploaded image is a File object which is also a Blob object.

Converting a Blob object to a Uint8Array:

blob.arrayBuffer().then((arrBuf) => {
  new Uint8Array(arrBuf)
})

Then, the encode method must return a promise which is resolved with the base64 string. It breaks current api.

Or, add a new encode method (base64/encodeFile) and a new decode method (base64/decodeFile).

Use cypress to test compatibility

Pros

  • The cypress.io has free plan
  • Has been used by many users.

Cons

  • Need to transform from karma to cypress
  • Does not support Safari and IE browsers (Both under consideration)

Maybe, it would be better to use cypress when it supports all major browsers.

url.parse method could not parse localhost url

micell.url.parse('http://localhost:8000/zh/docs/getting-started.html')

Output error:

micell@latest:1 Uncaught TypeError: invalid url
    at Object.fn [as parse] (micell@latest:1)
    at <anonymous>:1:12

Proposal: download

Download single file or multiple files is a common requirement in web development. In tradition, opening a new tab with the file url is a choice. But, we may need download file(s) in current page.

There are two solutions to figure out this problem.

Solution 1

Use download attribute in <a> tag.

Solution 2

Use solution like FileSaver.js.

Others:

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot 📦🚀

Proposal: date/parse

Parse a timestamp number or a parsable date string or a Date object to an object which has 'years', 'months', 'days', 'hours', 'minutes', 'seconds' and 'milliseconds' properties.

It can accept the second parameter isUTC: if isUTC is true, then UTC date will be used, or local date will be used.

Same as moment's toObject.

// GMT+0800

micell.date.parse(new Date(0))
// => { years: 1970, months: 0, dates: 1, hours: 8, minutes: 0, seconds: 0, milliseconds: 0 }

micell.date.parse(new Date(0), true)
// => { years: 1970, months: 0, dates: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }

Add security analysis

We can use github security feature which includes Dependabot alerts and Code scanning alerts.

Compatible with low versions

At present, the last 4 versions is not enough for some browsers, such as Safari, QQ Browser, Sogou Browser and 360 Browser.

The css() method returns an empty for "padding" property

This is not a bug. This is because of the limitation of CSSStyleDeclaration object. You can see the notes section in link https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle#Notes.

The returned CSSStyleDeclaration object contains active values for CSS property longhand names. For example, border-bottom-width instead of the border-width and border shorthand property names. It is safest to query values with only longhand names like font-size. Shorthand names like font will not work with most browsers.

So, we need change the below test cases of css method.

expect(css('#el1', 'padding')).to.equal('10px')

expect(style.borderWidth).to.equal('2px')

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.