{"version":3,"sources":["../../index.ts"],"sourcesContent":["// http://stackoverflow.com/questions/11935175/sampling-a-random-subset-from-an-array\nimport { Feature, FeatureCollection, Geometry, GeometryObject } from \"geojson\";\nimport { featureCollection } from \"@turf/helpers\";\n\n/**\n * Takes a {@link FeatureCollection} and returns a FeatureCollection with given number of {@link Feature|features} at random.\n *\n * @function\n * @param {FeatureCollection} featurecollection set of input features\n * @param {number} num number of features to select\n * @returns {FeatureCollection} a FeatureCollection with `n` features\n * @example\n * var points = turf.randomPoint(100, {bbox: [-80, 30, -60, 60]});\n *\n * var sample = turf.sample(points, 5);\n *\n * //addToMap\n * var addToMap = [points, sample]\n * turf.featureEach(sample, function (currentFeature) {\n * currentFeature.properties['marker-size'] = 'large';\n * currentFeature.properties['marker-color'] = '#000';\n * });\n */\nfunction sample(\n fc: FeatureCollection,\n num: number\n): FeatureCollection {\n if (!fc) throw new Error(\"fc is required\");\n if (num === null || num === undefined) throw new Error(\"num is required\");\n if (typeof num !== \"number\") throw new Error(\"num must be a number\");\n var outFC = featureCollection(getRandomSubarray(fc.features, num));\n return outFC;\n}\n\nfunction getRandomSubarray(\n arr: Feature[],\n size: number\n) {\n var shuffled = arr.slice(0),\n i = arr.length,\n min = i - size,\n temp,\n index;\n while (i-- > min) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(min);\n}\n\nexport { sample };\nexport default sample;\n"],"mappings":";AAEA,SAAS,yBAAyB;AAqBlC,SAAS,OACP,IACA,KACsB;AACtB,MAAI,CAAC,GAAI,OAAM,IAAI,MAAM,gBAAgB;AACzC,MAAI,QAAQ,QAAQ,QAAQ,OAAW,OAAM,IAAI,MAAM,iBAAiB;AACxE,MAAI,OAAO,QAAQ,SAAU,OAAM,IAAI,MAAM,sBAAsB;AACnE,MAAI,QAAQ,kBAAkB,kBAAkB,GAAG,UAAU,GAAG,CAAC;AACjE,SAAO;AACT;AAEA,SAAS,kBACP,KACA,MACA;AACA,MAAI,WAAW,IAAI,MAAM,CAAC,GACxB,IAAI,IAAI,QACR,MAAM,IAAI,MACV,MACA;AACF,SAAO,MAAM,KAAK;AAChB,YAAQ,KAAK,OAAO,IAAI,KAAK,KAAK,OAAO,CAAC;AAC1C,WAAO,SAAS,KAAK;AACrB,aAAS,KAAK,IAAI,SAAS,CAAC;AAC5B,aAAS,CAAC,IAAI;AAAA,EAChB;AACA,SAAO,SAAS,MAAM,GAAG;AAC3B;AAGA,IAAO,sBAAQ;","names":[]}