Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prep for oss #23

Merged
merged 1 commit into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
sudo: false
notifications:
email: false
node_js:
- "6"
- "8"
cache:
directories: # Cache dependencies
- node_modules
script:
- npm test
6 changes: 6 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code of conduct

**TL;DR**<br>
We expect folks that participate in both our online and [IRL](www.urbandictionary.com/define.php?term=IRL) communities to be kind and considerate of others at all times.

Esri's official CoC text can be found at http://www.esri.com/events/code-of-conduct
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Esri welcomes contributions from anyone and everyone. Please see our [guidelines for contributing](https://github.com/esri/contributing).
103 changes: 72 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# Adlib
# adlib

A library for interpolating property values in JSON Objects.
[![npm version][npm-img]][npm-url]
[![build status][travis-img]][travis-url]
[![apache licensed](https://img.shields.io/badge/license-Apache-green.svg?style=flat-square)](https://raw.githubusercontent.com/Esri/adlib/master/LICENSE)

The Hub team uses this to create customized Web Maps, Hub Sites, Hub Pages and other types of items.
[npm-img]: https://img.shields.io/npm/v/adlib.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/adlib
[travis-img]: https://img.shields.io/travis/Esri/adlib/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/Esri/adlib

> A library for interpolating property values in JSON Objects.

The [ArcGIS Hub](https://hub.arcgis.com) team uses adlib to create customized Web Maps, Hub Sites, Hub Pages and other items in ArcGIS Online.

[Live Demo](https://arcgis.github.io/ember-arcgis-adlib-service/)

# General Pattern
```

```js
template: {
val: '{{thing.val}}'
};
Expand All @@ -30,7 +42,7 @@ If the `obj.prop` "path" in the settings object is a string, that string value i
## Multiple Strings
A property of a template can have a value like `'The {{thing.animal}} was {{thing.color}}'`. When combined with a settings object that has the appropriate values, this will result in `The fox was brown`.

```
```js
let template = {
value: 'The {{thing.animal}} was {{thing.color}}'
};
Expand All @@ -43,10 +55,12 @@ let settings = {
let result = adlib(template, settings);
//> {value: 'The fox was red'}
```

## Objects

If the interpolated value is an object, it is returned. This allow us to graft trees of json together.

```
```js
let template = {
value: '{{s.obj}}'
};
Expand All @@ -64,7 +78,7 @@ let result = adlib(template, settings);
## Arrays
If the interpolated value is an array, it is returned. Interpolation is also done within arrays.

```
```js
let template = {
values: ['{{s.animal}}', 'fuzzy', '{{s.color}}'],
names: '{{s.names}}'
Expand All @@ -84,7 +98,7 @@ let result = adlib(template, settings);
## Transforms
Adlib can apply transforms during the interpolation. The transform fn should have the following signature: `fn(key, value, settings)`.

```
```js
// Pattern
// {{key:transformFnName}}

Expand All @@ -100,18 +114,16 @@ let settings = {
// key: s.animal.type
// value: 'bear'
// transformFnName: 'upcase'

```

### Notes About Transforms
- Transforms are ideally pure functions, and they **must** be sync functions! Promises are not supported.
- Transforms are ideally pure functions, and they **must** be sync functions! Promises are not supported.
- Transform functions should be VERY resilient - we recommend unit testing them extensively
- If your settings hash does not have an entry for the `key`, the `value` will be `null`.


### Transforms

```
```js
let template = {
value:'{{s.animal.type:upcase}}'
};
Expand All @@ -135,7 +147,7 @@ let result = adlib(template, settings, transforms);
### Transforms using the Key
A typical use-case for this is for translation.

```
```js
let template = {
value:'{{s.animal.type:translate}}'
};
Expand Down Expand Up @@ -163,7 +175,8 @@ let result = adlib(template, settings, transforms);
By default, if the key is not found, `adlib` simply leaves the `{{key.path}}` in the output json. However, that can/will lead to problems when the json is consumed.

The `optional` transform helps out in these scenarios. By default when `adlib` encounters something like:
```

```js
{
someProp: 'red'
val: '{{key.path:optional}}'
Expand All @@ -172,15 +185,15 @@ The `optional` transform helps out in these scenarios. By default when `adlib` e

and `key.path` is `null` or `undefined`, the `val` property will simply be removed.

```
```js
{
someProp: 'red'
}
```

The same thing works in arrays

```
```js
{
someProp: 'red'
vals: [
Expand All @@ -201,7 +214,7 @@ The same thing works in arrays

However, there are times when simply removing the property/entry is not enough. Sometimes you need to "reach up" the object graph and remove a parent. This is where the `levelToRemove` comes in...

```
```js
let template = {
someProp: 'red',
operationalLayers: [
Expand Down Expand Up @@ -229,6 +242,7 @@ let settings = {
operationalLayers: []
}
```

### levelToRemove

| value | removes what |
Expand All @@ -238,12 +252,11 @@ let settings = {
| 2 | the grand-parent object/array |
| ... | ... up the hiearchy |


### Path Hierarchies

Sometimes you may want to adlib a value using one of several possible data sources. You can specify each data source in a hierarchy of preference in the template

```
```js
let template = {
dataset: {
title: {{layer.name||item.title}},
Expand All @@ -268,15 +281,15 @@ let settings = {
a: {
weird: {
xml: {
doc: '1505836376836'
}
}
}
}
}
}
}
}
doc: '1505836376836'
}
}
}
}
}
}
}
}
}
},
item: {
Expand All @@ -298,8 +311,8 @@ let transforms = {
toISO: function (key, val, settings) {
if (isStringAndNotADateValue(val)) {
return new Date(val).toISOString()
}
}
}
}
}

adlib(template, settings, transforms)
Expand All @@ -325,7 +338,7 @@ We support returning strings ('RED', 'the red fox'), ints (23, 15), and floats (
**Note** Transforms can not be applied to the default value!
Please see TODO.md for notes about changes required for this.

```
```js
let template = {
msg: 'Site is at {{obj.mainUrl||obj.otherUrl||https://foo.bar?o=p&e=n}}'
}
Expand All @@ -336,3 +349,31 @@ let result = adlib(template, settings);
// => returns
// 'Site is at https://foo.bar?o=p&e=n'
```

### Local Development

```
npm install && npm test
```

### Contributing

Esri welcomes contributions from anyone and everyone. Please see our [guidelines for contributing](https://github.com/Esri/contributing/blob/master/CONTRIBUTING.md).

### License

Copyright 2017 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

> http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

A copy of the license is available in the repository's [LICENSE](./LICENSE) file.
9 changes: 5 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<!DOCTYPE html>
<html>
<head>
Expand All @@ -22,9 +23,9 @@
<ul class="nav navbar-nav">
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/ArcGIS/adlib/issues">Issues</a></li>
<li><a href="https://github.com/ArcGIS/adlib/blob/master/CHANGELOG.md">Change Log</a></li>
<li><a href="https://github.com/ArcGIS/adlib">Github Repo</a></li>
<li><a href="https://github.com/Esri/adlib/issues">Issues</a></li>
<li><a href="https://github.com/Esri/adlib/blob/master/CHANGELOG.md">Change Log</a></li>
<li><a href="https://github.com/Esri/adlib">Github Repo</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
Expand All @@ -36,6 +37,6 @@ <h1>Adlib: Json as Templates</h1>
</div>
</div>

<scipt src="./js/adlib.min.js"></scipt>
<scipt src="https://unpkg.com/[email protected]/dist/adlib.min.js"></scipt>
</body>
</html>
Loading