Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active Apr 8, 2019
Embed
What would you like to do?
Factory functions to create a value storage hidden in a closure

Closure Value

Factory functions to create a value storage hidden in a closure

  • singleValueFactory() - creates storage for a single value
  • valuesMapFactory() - creates storage with Map hidden, allows adding key/value pairs
  • valuesSetFactory() - creates storage with Set hidden, allows adding unique values

Demo on jsFiddle

singleValueFactory()

Returns object with methods

  • get() - retrieve current value
  • set(value) - store value

valuesMapFactory()

Returns object with methods

  • getDefault() - retrieve Map with default values
  • copy() - retrieve a copy of the Map with all currently stored values
  • delete(key) - delete value by key
  • has(key) - check if value exists by key
  • set(key, value) - store value by key
  • get(key) - retrieve value by key Additionaly to methods returned object contains values field with Map object used for storing values.

valuesSetFactory()

Returns object with methods

  • getDefault() - retrieve Set with default values
  • get() - retrieve a copy of Set with currently stored values
  • delete(value) - remove value from Set
  • has(value) - check if Set contains value
  • add(value) - add value to Set Additionaly to methods returned object contains values field with Set object used for storing values.
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const singleValueFactory = (defaultValue = null, valueFormatter = (value) => value) => {
let value = defaultValue;
return {
getDefault: () => defaultValue,
get: () => value,
set: (newValue = defaultValue) => {
value = valueFormatter(newValue);
},
};
};
const valuesMapFactory = (defaults = new Map(), valueFormatter = (key, value) => value) => {
const defaultValues = new Map(defaults);
const getDefault = () => new Map(defaultValues);
const values = getDefault();
return {
values,
getDefault,
copy: () => new Map(values),
delete: (key) => values.delete(key),
has: (key) => values.has(key),
set: (key, value) => values.set(key, valueFormatter(key, value)),
get: (key) => values.get(key),
};
};
const valuesSetFactory = (defaults = new Set(), valueFormatter = (value) => value) => {
const defaultValues = new Set(defaults);
const getDefault = () => new Set(defaultValues);
const values = getDefault();
return {
values,
getDefault,
get: () => new Set(values),
delete: (value) => values.delete(value),
has: (value) => values.has(value),
add: (value) => values.add(valueFormatter(value)),
};
};
exports.singleValueFactory = singleValueFactory;
exports.valuesMapFactory = valuesMapFactory;
exports.valuesSetFactory = valuesSetFactory;
(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? factory(exports)
: typeof define === 'function' && define.amd
? define(['exports'], factory)
: factory((global.ClosureValue = {}));
})(this, function(exports) {
'use strict';
const singleValueFactory = (defaultValue = null, valueFormatter = (value) => value) => {
let value = defaultValue;
return {
getDefault: () => defaultValue,
get: () => value,
set: (newValue = defaultValue) => {
value = valueFormatter(newValue);
},
};
};
const valuesMapFactory = (defaults = new Map(), valueFormatter = (key, value) => value) => {
const defaultValues = new Map(defaults);
const getDefault = () => new Map(defaultValues);
const values = getDefault();
return {
values,
getDefault,
copy: () => new Map(values),
delete: (key) => values.delete(key),
has: (key) => values.has(key),
set: (key, value) => values.set(key, valueFormatter(key, value)),
get: (key) => values.get(key),
};
};
const valuesSetFactory = (defaults = new Set(), valueFormatter = (value) => value) => {
const defaultValues = new Set(defaults);
const getDefault = () => new Set(defaultValues);
const values = getDefault();
return {
values,
getDefault,
get: () => new Set(values),
delete: (value) => values.delete(value),
has: (value) => values.has(value),
add: (value) => values.add(valueFormatter(value)),
};
};
exports.singleValueFactory = singleValueFactory;
exports.valuesMapFactory = valuesMapFactory;
exports.valuesSetFactory = valuesSetFactory;
Object.defineProperty(exports, '__esModule', { value: true });
});
{
"author": {
"name": "Oleg Galaburda",
"email": "burdiuz@gmail.com",
"url": "http://actualwave.com/"
},
"bugs": {
"url": "https://gist.github.com/burdiuz/949516f83dc01fcbba531a3d143c060d",
"email": "burdiuz@gmail.com"
},
"description": "Factory functions to create a value storage hidden in a closure",
"homepage": "https://gist.github.com/burdiuz/949516f83dc01fcbba531a3d143c060d",
"keywords": [
"js",
"javascript",
"closure",
"Map",
"Set",
"factory",
"storage"
],
"license": "MIT",
"main": "closure-value.js",
"name": "@actualwave/closure-value",
"version": "0.0.3"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment