# author: mario
# function: ConvertTo-PS1
# x-poshcode-id: 6831
# x-archived: 2017-05-16T03:55:11
# x-published: 2017-04-04T18:20:00
#
# encoding: utf-8
# api: ps
# type: functions
# category: data
# title: ConvertTo-PS1
# description: Transform nested hashtable into Powershell-literal (string)
# version: 0.5
# status: beta
# license: MITL
#
# Workaround implementation due to lack of ConvertTo/From-Json in PS 2.0.
# Useful to create a speedier cache, e.g. after extracting Excel files.
# Outfiles can be read in with `$hash = (. ./data/cachefn.ps1)` simply.
#
# 路 Only covers strings, integers, and hashtables
# 路 REALLY REALLY crude string filtering
# 路 Just didn't want to use less legible @'
'@
# 路 Definitely NOT SAFE to use on arbitrary input
#-- Transform nested hashtable into Powershell-literal (string)
function ConvertTo-PS1() {
param($hash, $indent="", $depth=100, $CRLF="`r`n", $Q="'", $v2=@{})
$sub = $indent + " "
switch ($hash.GetType().Name) {
Int32 {}
Double {}
String { $hash = $Q + ($hash -replace "[''拻``憫‛‛]","路") + $Q }
PSCustomObject {
$hash.PSObject.Properties | ? { $_.Name } | % { $v2[$_.Name] = $_.Value }
$hash = ConvertTo-PS1 $v2 $indent
}
Hashtable {
$hash = "@{$CRLF" + (($hash.keys | % {
$sub + (ConvertTo-PS1 $_ $sub) + " = " + (ConvertTo-PS1 $hash[$_] $sub)
}) -join ";$CRLF") + "$CRLF$indent}"
}
default { $hash = "$Q$value$Q"; }
}
return "$hash"
}