Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Jan 25, 2023
0 parents commit ad89ed2
Show file tree
Hide file tree
Showing 26 changed files with 5,012 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/**/*.*
35 changes: 35 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"prettier"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"prettier/prettier": ["error"],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
},
"overrides": [
{
"files": [ "test/**/*.test.js" ],
"globals": {
"describe": true,
"context": true,
"it": true,
"beforeEach": true,
"afterEach": true
}
}
],
"env": {
"browser": true,
"node": true
}
}
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
playground/**/* linguist-vendored
test/**/*.js linguist-vendored
45 changes: 45 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "CodeQL"

on:
push:
branches:
- main

pull_request:
branches:
- main

schedule:
- cron: "49 0 * * 6"

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [javascript]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: +security-and-quality

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests

on:
pull_request:
branches:
- '*'

push:
branches:
- main

jobs:
test:
name: JavaScript Test Action
runs-on: ubuntu-latest
strategy:
matrix:
node: [14, 16, 18]

steps:
- uses: actions/checkout@master

- name: Setup Node v${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: 'yarn'

- name: Yarn install
run: yarn install --frozen-lockfile

- name: Run JavaScript Tests
run: yarn build

- name: Run JavaScript Tests
run: yarn test

- name: Lint
run: yarn lint
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.tgz
*.tsbuildinfo
*.log
*~

/dist
node_modules/
coverage/

yarn-error.log

.DS_Store
.rollup.cache
.tool-versions
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.12.1
28 changes: 28 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.babelrc
.babelrc.js
.DS_Store
.gitignore
.yarn.lock

*.log
*.tsbuildinfo
*.tgz

README.md
rollup.config.js
web-test-runner.config.mjs
tsconfig.json
webpack.config.js
yarn-error.log
*~

/.git
/.github
/.gitattributes

/node_modules
/playground
/src
/test
/coverage
/assets
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"printWidth": 120,
"semi": false
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Marco Roth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Introduction

`current.js` is a tiny 🤏 JavaScript library (only 283 bytes when compressed) that allows you to access data stored in "current" HTML `<meta>` 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:

```diff
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 `<meta>` elements to the `<head>` section of your HTML document, with the `name` attribute starting with `current-`:

```html
<head>
<meta name="current-environment" content="production">
<meta name="current-user-id" content="123">
<meta name="current-user-time-zone-name" content="Central Time (US & Canada)">
</head>
```

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

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

If there are multiple `<meta>` 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
// => {}
```

Please note that if you use the constant import the function will not be globally available and you have to access it via the constant.

## 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!
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "current.js",
"version": "0.1.0",
"description": "On-demand JavaScript objects from 'current' HTML <meta> elements",
"main": "dist/current.js",
"module": "dist/current.js",
"unpkg": "dist/current.umd.js",
"types": "dist/types/index.d.ts",
"author": "Marco Roth",
"license": "MIT",
"repository": "https://github.com/marcoroth/current.js",
"sideEffects": false,
"scripts": {
"start": "cd playground && yarn start",
"prebuild": "yarn clean",
"build": "tsc --noEmit false --declaration true --emitDeclarationOnly true --outDir dist/types && rollup -c",
"watch": "rollup -wc",
"dev": "concurrently 'yarn run watch' 'yarn run start'",
"clean": "rimraf dist",
"prerelease": "yarn build",
"test": "web-test-runner test/**/*.test.js",
"lint": "eslint .",
"format": "yarn lint --fix"
},
"devDependencies": {
"@open-wc/testing": "^3.1.7",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.0.0",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"@web/test-runner": "^0.15.0",
"concurrently": "^7.3.0",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.3",
"rimraf": "^3.0.2",
"rollup": "^3.10.1",
"rollup-plugin-filesize": "^9.1.2",
"rollup-plugin-terser": "^7.0.2",
"tslib": "^2.4.0",
"typescript": "^4.9.4"
}
}
17 changes: 17 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Current.js Playground</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<meta name="current-environment" content="production">
<meta name="current-user-id" content="123">
<meta name="current-user-time-zone-name" content="Central Time (US & Canada)">

<script type="module" src="/index.js"></script>
</head>

<body></body>
</html>
5 changes: 5 additions & 0 deletions playground/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "current.js"

console.log("Current.environment", Current.environment)
console.log("Current.user", Current.user)
console.log("Current.foo", Current.foo)
20 changes: 20 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "current-js-playground",
"private": true,
"version": "0.1.0",
"description": "A playground for Current.js",
"license": "MIT",
"repository": "https://github.com/marcoroth/current.js",
"dependencies": {
"current.js": "link:../"
},
"scripts": {
"start": "yarn run dev",
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"vite": "^3.0.2"
}
}
10 changes: 10 additions & 0 deletions playground/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from "path"

export default {
resolve: {
alias: {
"current.js": path.resolve(__dirname, "../dist/index"),
},
},
plugins: [],
}
Loading

0 comments on commit ad89ed2

Please sign in to comment.