Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/marcoroth/current.js

A tiny 🀏 library (only 410 B) that allows you to access current `<meta>` elements
https://github.com/marcoroth/current.js

Last synced: about 2 months ago
JSON representation

A tiny 🀏 library (only 410 B) that allows you to access current `<meta>` elements

Awesome Lists containing this project

README

        

Current.js






NPM Version


NPM Downloads


NPM Bundle Size

## Introduction

`current.js` is a tiny 🀏 JavaScript library (only 410 bytes) that allows you to access data stored in "current" HTML `` elements.

## Installation

To add `current.js` to your project, run the following command in your terminal:

```bash
yarn add current.js
```

Then, in the JavaScript file where you want to use `current.js` (usually `application.js`), add the following line of code:

```js
import "current.js"
```

This will make the `Current` object available globally, so you can access it in any file without having to import it again.

Alternatively, you can import the `Current` object directly:

```js
import { Current } from "current.js"
```

## Usage

To use `current.js`, you need to add `` elements to the `` section of your HTML document, with the `name` attribute starting with `current-`:

```html



```

You can then access the data stored in these elements using the `Current` object. If there is only one `` tag with the name you requested, `current.js` will return the value as a string:

```js
Current.environment
// => "production"
```

If there are multiple `` tags with the same name, `current.js` will return an object with the remaining name as a camelized key.

```js
Current.user
// => { id: "123", timeZoneName: "Central Time (US & Canada)" }
```

If the name you requested is not found, it will return an empty object

```js
Current.foo
// => {}
```

## Configuration

#### Custom Prefix

The `prefix` property on `config` can be used to customize the prefix used to lookup the `` tags.

```js
import { config }Β from "current.js"
config.prefix = "config"
```

```html

```

Calling `Current.environment` would now look for the meta tag with the `config-environment` name.

```js
Current.environment
// => "production"
```

#### No Prefix

You can also configure `current.js` to use no `prefix` if you set the `prefix` to `null`.

```js
import { config }Β from "current.js"
config.prefix = null
```

```html

```

Calling `Current.environment` would also return `"production"` in this case.

## Development

To run the test runner:

```bash
yarn install
yarn build
yarn test
```

## Acknowledgments

This library was inspired by the [source code](https://production.haystack-assets.com/assets/helpers/current_helpers-69434f7688aaf68b68226df19cd29426713fdcad.js) of [HEY.com](https://hey.com), developed by [37signals](https://37signals.com). The original source code can be found [here](https://production.haystack-assets.com/assets/helpers/current_helpers-69434f7688aaf68b68226df19cd29426713fdcad.js).

A big shout out to the team at 37signals and HEY.com for not minifying the JavaScript source code in their apps by ["Paying tribute to the web with View Source"](https://m.signalvnoise.com/paying-tribute-to-the-web-with-view-source/).

We have made some changes and improvements, but this library wouldn't have been possible without their inspiration and ideas. Thanks, it's really appreciated!