@guardian/libs
A collection of JavaScript libraries and TypeScript types for Guardian projects
Type that describes the role of an element in an article.
The Guardian's consent management platform.
API over document.cookies
.
Country data and methods to access it.
Codified editorial design and information architecture.
Get the user’s current location.
Get the active switches on theguardian.com.
Check whether a value is a boolean.
Check whether a value is a NonNullable
.
Checks whether a value is a plain object (i.e. {}
-like).
Check whether a value is one of a set of literal string or numbers.
Check whether a value is a string.
Check whether a value is undefined
.
Combines separate url parts into one valid url string.
Inject an external JavaScript file.
Selectively log team-specific messages to the console.
Types relating to Ophan.
API over window.performance
with hooks into log
the data warehouse.
Robust API over localStorage
and sessionStorage
.
Format absolute dates as time-ago strings.
Installation
yarn add @guardian/libs
or
npm install @guardian/libs
then
import { loadScript, storage, ...etc } from '@guardian/libs';
Bundling
This package uses ES2020
.
If your target environment does not support that, make sure you transpile this package when bundling your application.
Local Testing Using Linking
- In the command line in your terminal, navigate to the csnx root directory. Run:
make @guardian/libs:build
This build command has to be re-run for changes to be picked up.
- Navigate to the repo that will test your code changes. In the appropriate folder of that repo, run
npm/yarn/pnpm link ../csnx/dist/libs/@guardian/libs
where '../csnx' is the path to your local changes in csnx where you cloned the csnx code to.
After re-building the '@guardian/libs' in step 1. this command does not have to be repeated and changes will be automatically picked up.
- Once you’ve developed your changes and tested, you can unlink from the repo where you are testing the changes
npm/yarn/pnpm unlink ../csnx/dist/libs/@guardian/libs