datamap-interface
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -11,7 +11,7 @@ /** | ||
* | ||
* @param {Record.<string, Item>} [values] | ||
* @param {Record<string, Item>|undefined} [values] | ||
*/ | ||
constructor(values?: Record<string, Item>) | ||
/** @type Record.<string, Item> */ | ||
map: Record<string, Item> | ||
constructor(values?: Record<string, Item> | undefined) | ||
/** @type {Record<string, Item|undefined>} */ | ||
map: Record<string, Item | undefined> | ||
/** | ||
@@ -23,7 +23,10 @@ * Add values to map. | ||
* | ||
* @param {string | Record.<string, Item>} values | ||
* @param {Item} [value] | ||
* @param {string|Record<string, Item>|undefined} [values] | ||
* @param {Item|undefined} [value] | ||
* @returns {this} | ||
*/ | ||
add(values: string | Record<string, Item>, value?: Item): this | ||
add( | ||
values?: string | Record<string, Item> | undefined, | ||
value?: Item | undefined | ||
): this | ||
/** | ||
@@ -33,10 +36,11 @@ * Remove things from map by key. | ||
* | ||
* @param {string|string[]} keys One or more keys | ||
* @param {string|Array<string>} keys | ||
* One or more keys. | ||
* @return {this} | ||
*/ | ||
remove(keys: string | string[]): this | ||
remove(keys: string | Array<string>): this | ||
/** | ||
* Get values in map. | ||
* | ||
* @returns {Record.<string, Item>} Values | ||
* @returns {Record<string, Item>} Values | ||
*/ | ||
@@ -47,3 +51,3 @@ all(): Record<string, Item> | ||
* | ||
* @returns {Record.<string, Item>} Values | ||
* @returns {Record<string, Item>} Values | ||
*/ | ||
@@ -54,3 +58,3 @@ valueOf(): Record<string, Item> | ||
* | ||
* @returns {Record.<string, Item>} Values | ||
* @returns {Record<string, Item>} Values | ||
*/ | ||
@@ -62,3 +66,3 @@ toJSON(): Record<string, Item> | ||
* @param {string} key | ||
* @returns {Item?} Value | ||
* @returns {Item|null} Value | ||
*/ | ||
@@ -65,0 +69,0 @@ get(key: string): Item | null |
59
index.js
@@ -1,2 +0,2 @@ | ||
var own = {}.hasOwnProperty | ||
const own = {}.hasOwnProperty | ||
@@ -13,6 +13,6 @@ /** | ||
* | ||
* @param {Record.<string, Item>} [values] | ||
* @param {Record<string, Item>|undefined} [values] | ||
*/ | ||
constructor(values) { | ||
/** @type Record.<string, Item> */ | ||
/** @type {Record<string, Item|undefined>} */ | ||
this.map = {} | ||
@@ -28,12 +28,13 @@ this.add(values) | ||
* | ||
* @param {string | Record.<string, Item>} values | ||
* @param {Item} [value] | ||
* @param {string|Record<string, Item>|undefined} [values] | ||
* @param {Item|undefined} [value] | ||
* @returns {this} | ||
*/ | ||
add(values, value) { | ||
var key | ||
/** @type {string} */ | ||
let key | ||
if (typeof values === 'string') { | ||
this.map[values] = value | ||
} else { | ||
} else if (values) { | ||
for (key in values) { | ||
@@ -53,7 +54,8 @@ if (own.call(values, key) && values[key] !== undefined) { | ||
* | ||
* @param {string|string[]} keys One or more keys | ||
* @param {string|Array<string>} keys | ||
* One or more keys. | ||
* @return {this} | ||
*/ | ||
remove(keys) { | ||
var index = -1 | ||
let index = -1 | ||
@@ -74,12 +76,16 @@ if (typeof keys === 'string') { | ||
* | ||
* @returns {Record.<string, Item>} Values | ||
* @returns {Record<string, Item>} Values | ||
*/ | ||
all() { | ||
/** @type {Record.<string, Item>} */ | ||
var values = {} | ||
var key | ||
/** @type {Record<string, Item>} */ | ||
const values = {} | ||
/** @type {string} */ | ||
let key | ||
for (key in this.map) { | ||
if (own.call(this.map, key) && this.map[key] !== undefined) { | ||
values[key] = this.map[key] | ||
if (own.call(this.map, key)) { | ||
const value = this.map[key] | ||
if (value !== undefined) { | ||
values[key] = value | ||
} | ||
} | ||
@@ -94,3 +100,3 @@ } | ||
* | ||
* @returns {Record.<string, Item>} Values | ||
* @returns {Record<string, Item>} Values | ||
*/ | ||
@@ -104,3 +110,3 @@ valueOf() { | ||
* | ||
* @returns {Record.<string, Item>} Values | ||
* @returns {Record<string, Item>} Values | ||
*/ | ||
@@ -115,8 +121,13 @@ toJSON() { | ||
* @param {string} key | ||
* @returns {Item?} Value | ||
* @returns {Item|null} Value | ||
*/ | ||
get(key) { | ||
return own.call(this.map, key) && this.map[key] !== undefined | ||
? this.map[key] | ||
: null | ||
if (own.call(this.map, key)) { | ||
const value = this.map[key] | ||
if (value !== undefined) { | ||
return value | ||
} | ||
} | ||
return null | ||
} | ||
@@ -150,4 +161,6 @@ | ||
keys() { | ||
var result = [] | ||
var key | ||
/** @type {string[]} */ | ||
const result = [] | ||
/** @type {string} */ | ||
let key | ||
@@ -154,0 +167,0 @@ for (key in this.map) { |
{ | ||
"name": "datamap-interface", | ||
"version": "2.0.0", | ||
"description": "Simple interface for a map functioning as a database", | ||
"version": "2.0.1", | ||
"description": "Legacy package (use `Map`)", | ||
"license": "MIT", | ||
@@ -30,21 +30,24 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.52.0" | ||
}, | ||
"scripts": { | ||
"prepublishOnly": "npm run build && npm run format", | ||
"prebuild": "rimraf \"*.d.ts\"", | ||
"build": "tsc", | ||
"prepack": "npm run build && npm run format", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
}, | ||
"prettier": { | ||
@@ -59,7 +62,3 @@ "tabWidth": 2, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -66,0 +65,0 @@ "remarkConfig": { |
191
readme.md
# datamap-interface | ||
[![Build][build-badge]][build] | ||
[![Coverage][coverage-badge]][coverage] | ||
[![Downloads][downloads-badge]][downloads] | ||
[![Size][size-badge]][size] | ||
**Stability: Legacy**. | ||
This package is no longer recommended for use. | ||
It’s still covered by semantic-versioning guarantees and not yet deprecated, but | ||
use of this package should be avoided. | ||
Please use a [`Map`][Map]. | ||
A basic interface for a map. | ||
Legacy [documentation for this package][docs] is still available in Git. | ||
## Install | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
[npm][]: | ||
```sh | ||
npm install datamap-interface | ||
``` | ||
## Use | ||
```js | ||
import {DatamapInterface} from 'datamap-interface' | ||
var animals = new DatamapInterface({ | ||
shark: 'fish', | ||
tuna: 'fish', | ||
colugo: 'mammal', | ||
human: 'mammal' | ||
}) | ||
animals.get('human') // => 'mammal' | ||
animals.get('unicorn') // => null | ||
animals.add('unicorn', 'mammal').get('unicorn') // => 'mammal' | ||
animals.remove('unicorn').has('unicorn') // => false | ||
``` | ||
## API | ||
This package exports the following identifiers: `DatamapInterface`. | ||
There is no default export. | ||
### DatamapInterface(values) | ||
`DatamapInterface` is a constructor, which can be passed an object. | ||
```js | ||
import {DatamapInterface} from 'datamap-interface' | ||
var animals = new DatamapInterface({ | ||
unicorn: 'mystical creature', | ||
shark: 'fish', | ||
tuna: 'fish', | ||
colugo: 'mammal', | ||
human: 'mammal' | ||
}) | ||
``` | ||
### `DatamapInterface([values])` | ||
Create a new instance. | ||
Values are passed to [`#add()`][add]. | ||
###### Example | ||
```js | ||
import {DatamapInterface} from 'datamap-interface' | ||
var animals = new DatamapInterface({ | ||
unicorn: 'mystical creature', | ||
shark: 'fish', | ||
tuna: 'fish', | ||
colugo: 'mammal', | ||
human: 'mammal' | ||
}) | ||
``` | ||
### `DatamapInterface#has(value)` | ||
### `DatamapInterface#is(value)` | ||
Check if `value` is in the map. | ||
###### Example | ||
```js | ||
animals.has('unicorn') // => true | ||
animals.has('rainbow') // => false | ||
``` | ||
### `DatamapInterface#get(key)` | ||
Get the value of `key`, or `null`. | ||
###### Example | ||
```js | ||
animals.get('unicorn') // => 'mystical creature' | ||
animals.get('rainbow') // => null | ||
``` | ||
### `DatamapInterface#add(values)` | ||
Add each value, or one pair. | ||
###### Example | ||
```js | ||
animals.add('giant grouper', 'fish') | ||
animals.add({dragon: 'mystical creature'}) | ||
``` | ||
### `DatamapInterface#remove([values])` | ||
Remove each value. | ||
###### Example | ||
```js | ||
animals.remove(['giant grouper', 'human']) | ||
animals.remove('dragon') | ||
``` | ||
### `DatamapInterface#keys()` | ||
Get each key in the map. | ||
###### Example | ||
```js | ||
animals.keys() // => ['shark', 'tuna', 'colugo', 'unicorn'] | ||
``` | ||
### `DatamapInterface#all()` | ||
### `DatamapInterface#valueOf()` | ||
### `DatamapInterface#toJSON()` | ||
Return the list as an `Object`. | ||
###### Example | ||
```js | ||
animals.all() | ||
``` | ||
Yields: | ||
```js | ||
{ | ||
shark: 'fish', | ||
tuna: 'fish', | ||
colugo: 'mammal', | ||
unicorn: 'mystical creature' | ||
} | ||
``` | ||
## Related | ||
* [datalist-interface](https://github.com/wooorm/datalist-interface) | ||
— Basic interface for a list functioning as a database | ||
## License | ||
@@ -174,20 +17,2 @@ | ||
[build-badge]: https://github.com/wooorm/datamap-interface/workflows/main/badge.svg | ||
[build]: https://github.com/wooorm/datamap-interface/actions | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/datamap-interface.svg | ||
[coverage]: https://codecov.io/github/wooorm/datamap-interface | ||
[downloads-badge]: https://img.shields.io/npm/dm/datamap-interface.svg | ||
[downloads]: https://www.npmjs.com/package/datamap-interface | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/datamap-interface.svg | ||
[size]: https://bundlephobia.com/result?p=datamap-interface | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[license]: license | ||
@@ -197,2 +22,4 @@ | ||
[add]: #datamapinterfaceaddvalues | ||
[docs]: https://github.com/wooorm/datamap-interface/tree/5d08b39 | ||
[map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8
232
0
8510
24