11import React , { Component } from "react" ;
22import PropTypes from "prop-types" ;
33
4+ import { genConfig } from "./utils" ;
5+
46import Face from "./face" ;
57import Hair from "./hair" ;
68import Hat from "./hat" ;
@@ -12,42 +14,6 @@ import Nose from "./nose";
1214import Mouth from "./mouth" ;
1315import Shirt from "./shirt" ;
1416
15- const sex = [ "man" , "woman" ] ;
16- const faceColor = [ "#F9C9B6" , "#AC6651" ] ;
17- const earSize = [ "small" , "big" ] ;
18- const hairColor = [ "#000" , "#fff" , "#77311D" , "#FC909F" , "#D2EFF3" , "#506AF4" , "#F48150" ] ;
19- const hairStyleMan = [ "normal" , "thick" , "mohawk" ] ;
20- const hairStyleWoman = [ "normal" , "womanLong" , "womanShort" ] ;
21- const hatColor = [ "#000" , "#fff" , "#77311D" , "#FC909F" , "#D2EFF3" , "#506AF4" , "#F48150" ] ;
22- const hatStyle = [ "beanie" , "turban" , "none" ] ;
23- const eyeBrowWoman = [ "up" , "upWoman" ] ;
24- const eyeStyle = [ "circle" , "oval" , "smile" ] ;
25- const noseStyle = [ "short" , "long" , "round" ] ;
26- const mouthStyle = [ "laugh" , "smile" , "peace" ] ;
27- const shirtStyle = [ "hoody" , "short" , "polo" ] ;
28- const shirtColor = [ "#9287FF" , "#6BD9E9" , "#FC909F" , "#F4D150" , "#77311D" ] ;
29- const bgColor = [ "#9287FF" , "#6BD9E9" , "#FC909F" , "#F4D150" , "#E0DDFF" , "#D2EFF3" , "#FFEDEF" , "#FFEBA4" , "#506AF4" , "#F48150" , "#74D153" ] ;
30- const glassesStyle = [ "round" , "square" , "none" ] ;
31-
32- const _pickRandomFromList = ( data , { avoidList = [ ] , usually = [ ] } = { } ) => {
33- // Filter out avoid options
34- const avoidSet = new Set (
35- avoidList . filter ( ( item ) => Boolean ( item ) )
36- ) ;
37- let myData = data . filter ( ( item ) => ! avoidSet . has ( item ) ) ;
38-
39- // Increase selecting possibility of usually options
40- const usuallyData = usually
41- . filter ( ( item ) => Boolean ( item ) )
42- . reduce ( ( acc , cur ) => acc . concat ( new Array ( 15 ) . fill ( cur ) ) , [ ] ) ;
43- myData = myData . concat ( usuallyData ) ;
44-
45- // Pick randon one from the list
46- const amount = myData . length ;
47- const randomIdx = Math . floor ( Math . random ( ) * amount ) ;
48- return myData [ randomIdx ] ;
49- } ;
50-
5117export default class ReactNiceAvatar extends Component {
5218 static propTypes = {
5319 id : PropTypes . string ,
@@ -160,69 +126,4 @@ export default class ReactNiceAvatar extends Component {
160126 }
161127}
162128
163- export const genConfig = ( userConfig = { } ) => {
164- const response = { } ;
165-
166- response . sex = userConfig . sex || _pickRandomFromList ( sex ) ;
167- response . faceColor = userConfig . faceColor || _pickRandomFromList ( faceColor ) ;
168- response . earSize = userConfig . earSize || _pickRandomFromList ( earSize ) ;
169- response . eyeStyle = userConfig . eyeStyle || _pickRandomFromList ( eyeStyle ) ;
170- response . noseStyle = userConfig . noseStyle || _pickRandomFromList ( noseStyle ) ;
171- response . mouthStyle = userConfig . mouthStyle || _pickRandomFromList ( mouthStyle ) ;
172- response . shirtStyle = userConfig . shirtStyle || _pickRandomFromList ( shirtStyle ) ;
173- response . glassesStyle = userConfig . glassesStyle || _pickRandomFromList ( glassesStyle , { usually : [ "none" ] } ) ;
174-
175- // Hair
176- let hairColorAvoidList = [ ] ;
177- let hairColorUsually = [ ] ;
178- if ( ! userConfig . hairColor ) {
179- switch ( response . sex ) {
180- case "woman" : {
181- hairColorAvoidList = response . faceColor === faceColor [ 1 ] && [ "#77311D" ] || [ ] ;
182- break ;
183- }
184- case "man" : {
185- hairColorUsually = [ "#000" ] ;
186- }
187- }
188- }
189- response . hairColor = userConfig . hairColor || _pickRandomFromList ( hairColor , {
190- avoidList : hairColorAvoidList ,
191- usually : hairColorUsually
192- } ) ;
193-
194- let myHairStyle = userConfig . hairStyle ;
195- if ( ! myHairStyle ) {
196- switch ( response . sex ) {
197- case "man" : {
198- myHairStyle = _pickRandomFromList ( hairStyleMan , { usually : [ "normal" , "thick" ] } ) ;
199- break ;
200- }
201- case "woman" : {
202- myHairStyle = _pickRandomFromList ( hairStyleWoman ) ;
203- break ;
204- }
205- }
206- }
207- response . hairStyle = myHairStyle ;
208-
209- // Hat
210- response . hatStyle = userConfig . hatStyle || _pickRandomFromList ( hatStyle , { usually : [ "none" ] } ) ;
211- response . hatColor = userConfig . hatColor || _pickRandomFromList ( hatColor ) ;
212- const _hairOrHatColor = response . hatStyle === "none" && response . hairColor || response . hatColor ;
213-
214- // Eyebrow
215- let myEyeBrowStyle = userConfig . eyeBrowStyle || "up" ;
216- if ( ! userConfig . eyeBrowStyle && response . sex === "woman" ) {
217- myEyeBrowStyle = _pickRandomFromList ( eyeBrowWoman ) ;
218- }
219- response . eyeBrowStyle = myEyeBrowStyle ;
220-
221- // Shirt color
222- response . shirtColor = userConfig . shirtColor || _pickRandomFromList ( shirtColor , { avoidList : [ _hairOrHatColor ] } ) ;
223-
224- // Background color
225- response . bgColor = userConfig . bgColor || _pickRandomFromList ( bgColor , { avoidList : [ _hairOrHatColor , response . shirtColor ] } ) ;
226-
227- return response ;
228- } ;
129+ export { genConfig } from "./utils" ;
0 commit comments