<commit_before>//
//  PlacesList.swift
//  Places
//
//  Copyright © 2017 Gemini Solutions. All rights reserved.
//

import Foundation
import DataStore

public class PlacesList: DataStoreContentJSONArray<[String:Any]> {
    public var places: [Place] {
        var places: [Place] = []
        content.forEach {
            if let place = place(from: $0) { places.append(place) }
        }
        return places
    }

    public override init() {
        super.init()
    }

    public init(placesData: [(place: Place.JSONObjectType, id: Place.PlaceIdType, lastUpdate: Date)]) {
        let places = placesData.map({ return ["place":$0.place, "id":Place.stringFromPlaceId($0.id), "lastUpdate":DataStore.dateString(from: $0.lastUpdate)] })
        super.init(json: places)
    }

    public func place(for id: Place.PlaceIdType) -> Place? {
        for data in content {
            if let idStr = data["id"] as? String, let placeId = Place.placeIdFromString(idStr), placeId == id {
                return place(from: data)
            }
        }
        return nil
    }

    public func append(place: Place) {
        guard let id = place.id, let lastUpdate = place.lastUpdate else { return }
        append(["place":place.content, "id":Place.stringFromPlaceId(id), "lastUpdate":DataStore.dateString(from: lastUpdate)])
    }

    private func place(from data: [String:Any]) -> Place? {
        guard let content = data["place"] as? Place.JSONObjectType, let id = data["id"] as? String, let lastUpdate = data["lastUpdate"] as? String else { return nil }
        guard let placeId = Place.placeIdFromString(id), let placeLastUpdate = DataStore.date(from: lastUpdate) else { return nil }
        guard let place = Place(content: content) else { return nil }
        place.id = placeId
        place.lastUpdate = placeLastUpdate
        return place
    }
}<commit_msg>[fix]<commit_after>//
//  PlacesList.swift
//  Places
//
//  Copyright © 2017 Gemini Solutions. All rights reserved.
//

import Foundation
import DataStore

public class PlacesList: DataStoreContentJSONArray<[String:Any]> {
    public var places: [Place] {
        var places: [Place] = []
        content.forEach {
            if let place = place(from: $0) { places.append(place) }
        }
        return places
    }

    public override init() {
        super.init()
    }

    public init(placesData: [(place: Place.JSONObjectType, id: Place.PlaceIdType, lastUpdate: Date)]) {
        let places = placesData.map({ return ["place":$0.place, "id":Place.stringFromPlaceId($0.id), "lastUpdate":PlacesList.dateString(from: $0.lastUpdate)] })
        super.init(json: places)
    }

    public func place(for id: Place.PlaceIdType) -> Place? {
        for data in content {
            if let idStr = data["id"] as? String, let placeId = Place.placeIdFromString(idStr), placeId == id {
                return place(from: data)
            }
        }
        return nil
    }

    public func append(place: Place) {
        guard let id = place.id, let lastUpdate = place.lastUpdate else { return }
        append(["place":place.content, "id":Place.stringFromPlaceId(id), "lastUpdate":PlacesList.dateString(from: lastUpdate)])
    }

    private func place(from data: [String:Any]) -> Place? {
        guard let content = data["place"] as? Place.JSONObjectType, let id = data["id"] as? String, let lastUpdate = data["lastUpdate"] as? String else { return nil }
        guard let placeId = Place.placeIdFromString(id), let placeLastUpdate = PlacesList.date(from: lastUpdate) else { return nil }
        guard let place = Place(content: content) else { return nil }
        place.id = placeId
        place.lastUpdate = placeLastUpdate
        return place
    }
}
{'fraction_non_alphanumeric': 0.12429681221537638, 'fraction_numerical': 0.004286096972944013, 'mean_word_length': 2.7377377377377377, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6578043', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1073, 'n_words': 300}