Browse Source

[build] Added build script

tags/v0.10.0
Muthu Kumar 6 years ago
parent
commit
03e00213e0
  1. 12
      build.sh
  2. 142
      es5/index.js
  3. 1
      es5/index.min.js
  4. 64
      package.json
  5. 2
      spec/PromiseObject.spec.js

12
build.sh

@ -0,0 +1,12 @@
#!/bin/bash
mkdir -p ./es5
echo "Browserifying..."
npx browserify ./es6/index.js -s promiseObject > ./es5/index.temp.js
echo "Babelifying..."
npx babel ./es5/index.temp.js --presets=env --plugins=babel-plugin-transform-object-rest-spread -o ./es5/index.js
echo "Minifying output..."
npx uglifyjs ./es5/index.js > ./es5/index.min.js
echo "Cleaning up..."
rm ./es5/index.temp.js
echo "Done!"

142
es5/index.js

@ -0,0 +1,142 @@
"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
(function (f) {
if ((typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object" && typeof module !== "undefined") {
module.exports = f();
} else if (typeof define === "function" && define.amd) {
define([], f);
} else {
var g;if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
g = this;
}g.promiseObject = f();
}
})(function () {
var define, module, exports;return function () {
function r(e, n, t) {
function o(i, f) {
if (!n[i]) {
if (!e[i]) {
var c = "function" == typeof require && require;if (!f && c) return c(i, !0);if (u) return u(i, !0);var a = new Error("Cannot find module '" + i + "'");throw a.code = "MODULE_NOT_FOUND", a;
}var p = n[i] = { exports: {} };e[i][0].call(p.exports, function (r) {
var n = e[i][1][r];return o(n || r);
}, p, p.exports, r, e, n, t);
}return n[i].exports;
}for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) {
o(t[i]);
}return o;
}return r;
}()({ 1: [function (require, module, exports) {
module.exports = require('../index.js');
}, { "../index.js": 2 }], 2: [function (require, module, exports) {
"use strict";
/**
* Returns true if x is an object, false otherwise.
* @param {any} x
* @returns {Boolean}
*/
var isObject = function isObject(x) {
return x && (typeof x === "undefined" ? "undefined" : _typeof(x)) === 'object' && x.constructor === Object;
};
/* A well known Symbol. */
var $SELF = typeof Symbol !== 'undefined' ? Symbol('SELF') : '[~~//-- SELF --//~~]';
/**
* Replaces values that match the query parameter
* with a reference to the parent parameter.
* @param {Object} object Object to make cyclic.
* @param {any} query Query to match against.
* @returns {Object}
*/
var makeCyclic = function makeCyclic(object, query) {
var start = function start(obj) {
return Object.keys(obj).reduce(function (acc, key) {
var value = obj[key];
if (value === query) {
obj[key] = object;
return [].concat(_toConsumableArray(acc), [key]);
};
if (isObject(value)) return [].concat(_toConsumableArray(acc), _toConsumableArray(start(value, query)));else return acc;
}, []);
};
return start(object);
};
/**
* Promise.map polyfill.
* @param {Array.<any>} arr Array of Promises.
* @param {Function} functor Function to call resolved values.
*/
var PromiseMap = function PromiseMap(arr, functor) {
return Promise.all(arr.map(function (x) {
return Promise.resolve(x).then(functor);
}));
};
/**
* Resolve a flat object's promises.
* @param {Object}
* @returns {Object}
*/
var ResolveObject = function ResolveObject(obj) {
return Promise.all(Object.keys(obj).map(function (key) {
return Promise.resolve(obj[key]).then(function (val) {
return obj[key] = val;
});
})).then(function (_) {
return obj;
});
};
/**
* Recursively resolves deep objects with nested promises.
* @param {Object} object Object or value to resolve.
* @returns {Object} Resolved object.
*/
var PromiseObject = function PromiseObject(object) {
var shouldReplaceSelf = false;
var ResolveDeepObject = function ResolveDeepObject(obj) {
return Promise.resolve(obj).then(function (resolvedObject) {
if (Array.isArray(resolvedObject)) {
// Promise and map every item to recursively deep resolve.
return PromiseMap(resolvedObject, function (obj) {
return ResolveDeepObject(obj);
});
} else if (isObject(resolvedObject)) {
return ResolveObject(Object.keys(resolvedObject).reduce(function (acc, key) {
if (resolvedObject[key] === object) {
shouldReplaceSelf = true;
return _extends({}, acc, _defineProperty({}, key, $SELF));
}
return _extends({}, acc, _defineProperty({}, key, ResolveDeepObject(resolvedObject[key])));
}, {}));
}
return resolvedObject;
});
};
return ResolveDeepObject(object).then(function (obj) {
// Replace $SELF with reference to obj
if (shouldReplaceSelf) makeCyclic(obj, $SELF);
return obj;
});
};
module.exports = PromiseObject;
}, {}] }, {}, [1])(1);
});

1
es5/index.min.js

@ -0,0 +1 @@
"use strict";var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key]}}}return target};var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i]}return arr2}else{return Array.from(arr)}}(function(f){if((typeof exports==="undefined"?"undefined":_typeof(exports))==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.promiseObject=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++){o(t[i])}return o}return r}()({1:[function(require,module,exports){module.exports=require("../index.js")},{"../index.js":2}],2:[function(require,module,exports){"use strict";var isObject=function isObject(x){return x&&(typeof x==="undefined"?"undefined":_typeof(x))==="object"&&x.constructor===Object};var $SELF=typeof Symbol!=="undefined"?Symbol("SELF"):"[~~//-- SELF --//~~]";var makeCyclic=function makeCyclic(object,query){var start=function start(obj){return Object.keys(obj).reduce(function(acc,key){var value=obj[key];if(value===query){obj[key]=object;return[].concat(_toConsumableArray(acc),[key])}if(isObject(value))return[].concat(_toConsumableArray(acc),_toConsumableArray(start(value,query)));else return acc},[])};return start(object)};var PromiseMap=function PromiseMap(arr,functor){return Promise.all(arr.map(function(x){return Promise.resolve(x).then(functor)}))};var ResolveObject=function ResolveObject(obj){return Promise.all(Object.keys(obj).map(function(key){return Promise.resolve(obj[key]).then(function(val){return obj[key]=val})})).then(function(_){return obj})};var PromiseObject=function PromiseObject(object){var shouldReplaceSelf=false;var ResolveDeepObject=function ResolveDeepObject(obj){return Promise.resolve(obj).then(function(resolvedObject){if(Array.isArray(resolvedObject)){return PromiseMap(resolvedObject,function(obj){return ResolveDeepObject(obj)})}else if(isObject(resolvedObject)){return ResolveObject(Object.keys(resolvedObject).reduce(function(acc,key){if(resolvedObject[key]===object){shouldReplaceSelf=true;return _extends({},acc,_defineProperty({},key,$SELF))}return _extends({},acc,_defineProperty({},key,ResolveDeepObject(resolvedObject[key])))},{}))}return resolvedObject})};return ResolveDeepObject(object).then(function(obj){if(shouldReplaceSelf)makeCyclic(obj,$SELF);return obj})};module.exports=PromiseObject},{}]},{},[1])(1)});

64
package.json

@ -1,31 +1,37 @@
{ {
"name": "@codefeathers/promise.object", "name": "@codefeathers/promise.object",
"version": "0.9.1", "version": "0.9.5",
"description": "Deep resolve promises in objects.", "description": "Deep resolve promises in objects.",
"main": "es6/index.js", "main": "es5/index.min.js",
"scripts": { "scripts": {
"test": "jasmine" "test": "jasmine",
}, "build": "./build.sh"
"keywords": [ },
"Promise", "keywords": [
"A+", "Promise",
"objects", "A+",
"deep resolve" "objects",
], "deep resolve"
"author": "Muthu Kumar <@MKRhere> (https://mkr.pw)", ],
"contributors": [ "author": "Muthu Kumar <@MKRhere> (https://mkr.pw)",
"Thomas Rory Gummerson <@TRGWII> (https://rory.no)" "contributors": [
], "Thomas Rory Gummerson <@TRGWII> (https://rory.no)"
"repository": { ],
"type": "git", "repository": {
"url": "git+https://github.com/codefeathers/Promise.object.git" "type": "git",
}, "url": "git+https://github.com/codefeathers/Promise.object.git"
"bugs": { },
"url": "https://github.com/codefeathers/Promise.object/issues" "bugs": {
}, "url": "https://github.com/codefeathers/Promise.object/issues"
"homepage": "https://github.com/codefeathers/Promise.object#readme", },
"license": "MIT", "homepage": "https://github.com/codefeathers/Promise.object#readme",
"devDependencies": { "license": "MIT",
"jasmine": "^3.1.0" "devDependencies": {
} "babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"browserify": "^16.2.2",
"jasmine": "^3.1.0",
"uglify-js": "^3.4.0"
}
} }

2
spec/PromiseObject.spec.js

@ -1,6 +1,6 @@
'use strict'; 'use strict';
Promise.object = require('../es6'); Promise.object = require('../es5/index.min.js');
// Simple object // Simple object
const testObject1 = { const testObject1 = {

Loading…
Cancel
Save