react-native-user-avatar
Version:
React Native component for a user avatar with fallback to colored initials
2 lines • 2.83 kB
JavaScript
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.getContainerStyle=exports.generateBackgroundColor=exports.generateBackgroundStyle=exports.fetchImage=exports.sumChars=exports.abbr=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _reactNative=require("react-native");var _initials=_interopRequireDefault(require("initials"));var _nodeFetch=_interopRequireDefault(require("node-fetch"));var abbr=function abbr(name,noUpperCase){var abbr=(0,_initials.default)(name,noUpperCase);if(name.startsWith('+')){abbr="+".concat(abbr);}if(!abbr){console.warn('Could not get abbr from name');abbr=name;}if(abbr.length>2){abbr=abbr.substring(0,2);}if(!noUpperCase){abbr=abbr.toUpperCase();}return abbr;};exports.abbr=abbr;var sumChars=function sumChars(str){var sum=0;for(var i=0;i<str.length;i++){sum+=str.charCodeAt(i);}return sum;};exports.sumChars=sumChars;var fetchImage=function fetchImage(src,options){var fetchCall,contentTypeHeader;return _regenerator.default.async(function fetchImage$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.prev=0;_context.next=3;return _regenerator.default.awrap((0,_nodeFetch.default)(src,options));case 3:fetchCall=_context.sent;contentTypeHeader=_reactNative.Platform.OS==='web'?fetchCall.headers.get('content-type'):fetchCall.headers.map['content-type'];if(!contentTypeHeader.startsWith('image/')){_context.next=9;break;}return _context.abrupt("return",true);case 9:console.warn('Online fetched source is not a supported image');return _context.abrupt("return",false);case 11:_context.next=17;break;case 13:_context.prev=13;_context.t0=_context["catch"](0);console.warn('Error fetching source, falling back to initials',_context.t0);return _context.abrupt("return",false);case 17:case"end":return _context.stop();}}},null,null,[[0,13]],Promise);};exports.fetchImage=fetchImage;var generateBackgroundStyle=function generateBackgroundStyle(name,bgColor,bgColors){var background;if(bgColor){background=bgColor;}else{var i=sumChars(name)%bgColors.length;background=bgColors[i];}return{backgroundColor:background};};exports.generateBackgroundStyle=generateBackgroundStyle;var generateBackgroundColor=function generateBackgroundColor(name,bgColor,bgColors){var background;if(bgColor){background=bgColor;}else{var i=sumChars(name)%bgColors.length;background=bgColors[i];}return background;};exports.generateBackgroundColor=generateBackgroundColor;var getContainerStyle=function getContainerStyle(size,src,borderRadius){return{borderRadius:borderRadius?borderRadius:size*0.5,borderWidth:src?0:1,borderColor:'transparent',justifyContent:'center',alignItems:'center'};};exports.getContainerStyle=getContainerStyle;
//# sourceMappingURL=helpers.js.map