# nanopage

> handy utility methods for traversing content state

Latest version **0.2.0** (published 2018-05-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install nanopage
pnpm add nanopage
yarn add nanopage
bun add nanopage
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2018-05-20 |
| First published | 2018-04-19 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 25.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Jon-Kyle |
| Maintainers | jondashkyle |
| Keywords | state |

## Links

- npm: https://www.npmjs.com/package/nanopage
- Repository: https://github.com/jondashkyle/nanopage
- Homepage: https://github.com/jondashkyle/nanopage#readme
- Issues: https://github.com/jondashkyle/nanopage/issues
- npm.io page: https://npm.io/package/nanopage

## Dependencies (4)

- [xtend](https://npm.io/package/xtend.md) ^4.0.1
- [object-keys](https://npm.io/package/object-keys.md) ^1.0.11
- [resolve-path](https://npm.io/package/resolve-path.md) ^1.4.0
- [object-values](https://npm.io/package/object-values.md) ^2.0.0

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2018-05-20
- 0.2.0-next2 (next) — 2018-05-24
- 0.2.0-next1 — 2018-05-21
- 0.0.2 — 2018-05-16
- 0.0.1 — 2018-05-16
- 0.0.1-next.3 — 2018-04-20
- 0.0.1-next.2 — 2018-04-19
- 0.0.1-next.1 — 2018-04-19

## README

<h1 align="center">nanopage</h1>

A super easy way of traversing flat content state. Pairs nicely with [`nanocontent`](https://github.com/jondashkyle/nanocontent).

```
npm i nanopage --save
```

## Example

```js
var Page = require('nanopage')

// some content state
var state = {
  href: '/',
  content: {
    '/': {
      title: 'Just a site',
      text: 'With some text',
      tags: ['and', 'some', 'tags']
    },
    '/example': {
      title: 'Example',
      text: 'Scope it out',
      great: {
        demo: 'Am I right?',
        nah: 'Ok nbd.'
      }
    }
  }
}

// instantiate the page
var page = new Page(state)

// directly access pages by their href
var site = page('/').value()
var about = page('/example').value()

// grab children and files
var children = page().children().sort('name', 'asc').value()
var files = page().files().value()

// create new queries from previous
var first = page(children).first().value()
var last = page(children).last().value()

// access specific keys
var lastTitle = page(last).value('title') // like this
var lastTitle = page(last).value().title // or like this
```

## Usage

- Every method (almost) is chainable
- End a query and return it’s value by calling `.value()` or `.v()`
- Values can be reused in new queries by doing `page(oldQuery)`

Want to transform a directory of files and folders into flat content state? Try [Nanocontent](https://github.com/jondashkyle/nanocontent)! Super handy to use with [Enoki](https://github.com/enokidotsite/enoki) and [Choo](https://github.com/choojs/choo). If using Choo, you might [not even need it](#extra).

## Philosophy

State is really handy. Especially global state. We ended up with state when webapps started to get really complex and we needed all of the interface to stay in sync with new data and minimize the amount of updates happening to the DOM.

After a while, we started using state for sites too. That is, sites that look more like sites and less like apps. Mostly because state is super handy. However, state can get really messy as sites get larger. Where do you store data? How do you reference it? If you have ten nested pages, do you have ten nested objects?

Instead of all of this complexity, lets reintroduce the URL. Each page url of your site is a key in a flat object. We can simply use the `window.location` to grab the data/content for the current page. Or, we can use any arbitrary url, like `/members/nelson`.

This way of organizing state for sites as a flat object of page urls makes it super trivial to access content in your views and pass it down into components, or whatever. Ok cool!

## Extra

Using Choo? Try the plugin! Don’t need all the bells and whistles? Try creating your own basic Choo plugin from scratch.

<details><summary><b>Use the Choo plugin</b></summary>

```js
var html = require('choo/html')
var choo = require('choo')
var app = choo()

app.use(require('nanopage/choo'))

app.route('*', function (state, emit) {
  return html`
    <body>${state.page().value('title')}</body>
  `
})

if (module.parent) module.exports = app
else app.mount('body')
```
</details>

<details><summary><b>Basic vanilla Choo plugin</b></summary>

```js
app.use(function (state, emitter) {
  state.page = function (key) {
    key = key || (state.href || '/')
    return state.content[key]
  }
})
```
</details>

## Methods

#### `.children()`

Alias for `.pages()`.

#### `.file(filename)`

Grab an individual file. For example, `.file('example.jpg')`.

#### `.files()`

Files of the current `page`.

#### `.find(href)`

Locate a `sub-page` of the `current page` based on the `href`.

#### `.first()`

Returns the first `page` or `file`.

#### `.hasView()`

Does the current page have a custom view?

#### `.images()`

Images of the current page.

#### `.isActive()`

Is the current page active? Returns boolean.

#### `.last()`

Returns the last `page` or `file`.

#### `.page()`

The current page.

#### `.pages()`

Sub-pages of the current page.

#### `.parent()`

The parent of the current page.

#### `.shuffle()`

Shuffle values of an array.

#### `.sort()`

Sorts the current value’s `.pages` by `.order`. Formatting of `.order` follows the arguments of `.sortBy` separated by a space. For example, `date asc`.

#### `.sortBy(key, order)`

Sort the `files` or `pages` based by a certain key. Order can be either `asc` or `desc`. For example, `.sortBy('name', 'desc')` or  `.sortBy('date', 'asc')`.

#### `.toArray()`

Converts the values of an object to an array.

#### `.v()`

Alias for `.value()`.

#### `.value()`

Return the current value. Not chainable.

#### `.visible()`

Returns if the current value key `visible` is not `false`.
</details>

---
_Source: https://npm.io/package/nanopage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
